2015-12-22 104 views
3

好了,由於某種原因,我得到:本地主機/ new_laravel_social_site /公/用戶/%7Busername%7D爲什麼我收到%7Busername%7D在我的網址的結尾

我應該得到這樣的: 本地主機/ new_laravel_social_site/public/user /用戶名

有很多代碼,但我正在做codecoarse laravel社交媒體網站。我點擊時間線上的人,然後出現。但是,如果我點擊他們的個人資料,那麼它工作得很好我會將代碼發佈到哪裏。

它打破了我{{$ reply-> user-> getNameOrUsername()}}的地方。我有一切工作,這與我使用$回覆。

編輯:所有路線現在在底部。

@foreach ($statuses as $status) 
      <div class="media"> 
      <a class="pull-left" href="{{ route('profile.index'), ['username' => $status->user->username] }}"> 
       <img class="media-object" alt="{{ $status->user->getNameOrUsername() }}" src=" {{ $status->user->getAvatarUrl(40) }}"> 
      </a> 
      <div class="media-body"> 
       <h4 class="media-heading"><a href="{{ route('profile.index'), ['username' => $status->user->username] }}">{{ $status->user->getNameOrUsername() }}</a></h4> 
       <p>{{ $status->body }}</p> 
       <ul class="list-inline"> 
        @if ($status->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1) 
        <li>{{ $status->created_at->format('j M Y, g:ia') }}</li> 
        @else 
        <li>{{ $status->created_at->diffForHumans()}}</li> 
        @endif 
        <li><a href="#">Like</a></li> 
        <li>10 likes</li> 
       </ul> 
       @foreach ($status->replies as $reply) 
       <div class="media"> 
        <a class="pull-left" href="{{ route('profile.index', ['username'=> $reply->user->username]) }}"> 
         <img class="media-object" alt="{{ $reply->user->getNameOrUsername() }}" src="{{ $reply->user->getAvatarUrl(30) }}"> 
        </a> 
        <div class="media-body"> 
         <h5 class="media-heading"><a href="{{ route('profile.index', ['username' => $reply->user->username]) }}">{{ $reply->user->getNameOrUsername() }}</a></h5> 
         <p>{{ $reply->body }}</p> 
         <ul class="list-inline"> 
          @if ($reply->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1) 
          <li>{{ $reply->created_at->format('j M Y, g:ia') }}</li> 
          @else 
          <li>{{ $reply->created_at->diffForHumans()}}</li> 
          @endif 
          <li><a href="#">Like</a></li> 
          <li>4 likes</li> 
         </ul> 
        </div> 
       </div> 
       @endforeach 
       <form role="form" action="{{ route('status.reply', ['statusId' => $status->id]) }}" method="post"> 
        <div class="form-group{{ $errors->has("reply-{$status->id}") ? ' has-error': '' }}"> 
         <textarea name="reply-{{ $status->id }}" class="form-control" rows="2" placeholder="Reply to this status"></textarea> 
         @if ($errors->has("reply-{$status->id}")) 
          <span class="help-block">{{ $errors->first("reply-{$status->id}")}}</span> 
         @endif 
        </div> 
        <input type="submit" value="Reply" class="btn btn-default btn-sm"> 
        <input type="hidden" name="_token" value="{{ Session::token() }}"> 
       </form> 
      </div> 
      </div> 
     @endforeach 
     {!! $statuses->render() !!} 
@foreach ($status->replies as $reply) 
       <div class="media"> 
        <a class="pull-left" href="{{ route('profile.index', ['username' => $user->username]) }}"> 
         <img class="media-object" alt="{{ $reply->user->getNameOrUsername() }}" src="{{ $reply->user->getAvatarUrl(30) }}"> 
        </a> 
        <div class="media-body"> 
         <h5 class="media-heading"><a href="{{ route('profile.index', ['username' => $reply->user->username]) }}">{{ $reply->user->username }}</a></h5> 
         <p>{{ $reply->body }}</p> 
         <ul class="list-inline"> 
          @if ($reply->created_at->diffInMonths(\Carbon\Carbon::now()) >= 1) 
          <li>{{ $reply->created_at->format('j M Y, g:ia') }}</li> 
          @else 
          <li>{{ $reply->created_at->diffForHumans()}}</li> 
          @endif 
          <li><a href="#">Like</a></li> 
          <li>4 likes</li> 
         </ul> 
        </div> 
       </div> 
       @endforeach 

模型

Stat.php:

<?php 

namespace Chatty\Models; 

use Illuminate\Database\Eloquent\Model; 


class Status extends Model 
{ 
    protected $table = 'statuses'; 

    protected $fillable = [ 
    'body' 
    ]; 

    public function user() 
    { 
    return $this->belongsTo('Chatty\Models\User', 'user_id'); 
    } 

    public function scopeNotReply($query) 
    { 
    return $query->whereNull('parent_id'); 
    } 

    public function replies() 
    { 
    return $this->hasMany('Chatty\Models\Status', 'parent_id'); 
    } 

} 

user.php的

<?php 

namespace Chatty\Models; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 

class User extends Model implements AuthenticatableContract 

{ 
    use Authenticatable; 


    protected $table = 'users'; 

    protected $fillable = [ 
     'username', 
     'email', 
     'password', 
     'confirm_password', 
     'first_name', 
     'last_name', 
     'location', 
    ]; 

    protected $hidden = [ 
     'password', 
     'confirm_password', 
     'remember_token', 
    ]; 

    public function getName() 
    { 
     if($this->first_name && $this->last_name) { 
     return "{$this->first_name} {$this->last_name}"; 
     } 

     if($this->first_name) { 
     return $this->first_name; 
     } 

     return null; 
    } 

    public function getNameOrUsername() 
    { 
     return $this->getName() ?: $this->username; 
    } 

    public function getFirstNameOrUsername() 
    { 
     return $this->first_name ?: $this->username; 
    } 

    public function getAvatarUrl($size) 
    { 
     return "https://www.gravatar.com/avatar/{{ md($this->email) }}?d=mm&s=$size"; 
    } 

    public function statuses() 
    { 
     return $this->hasMany('Chatty\Models\Status', 'user_id'); 
    } 

    public function friendsOfMine() 
    { 
     return $this->belongsToMany('Chatty\Models\User', 'friends', 'user_id', 'friend_id'); 
    } 

    public function friendOf() 
    { 
     return $this->belongsToMany('Chatty\Models\User', 'friends', 'friend_id', 'user_id'); 
    } 

    public function friends() 
    { 
     return $this->friendsOfMine()->wherePivot('accepted', true)->get()->merge($this->friendOf()->wherePivot('accepted', true)->get()); 
    } 

    public function friendRequest() 
    { 
     return $this->friendsOfMine()->wherePivot('accepted', false)->get(); 
    } 

    public function friendRequestPending() 
    { 
     return $this->friendOf()->wherePivot('accepted', false)->get(); 
    } 

    public function hasFriendRequestPending(User $user) 
    { 
     return (bool) $this->friendRequestPending()->where('id', $user->id)->count(); 
    } 

    public function hasFriendRequestRecieved(User $user) 
    { 
     return (bool) $this->friendRequest()->where('id', $user->id)->count(); 
    } 

    public function addFriend(User $user) 
    { 
     $this->friendOf()->attach($user->id); 
    } 

    public function acceptFriendRequest(User $user) 
    { 
     $this->friendRequest()->where('id', $user->id)->first()->pivot-> 
     update([ 
     'accepted' => true, 
     ]); 
    } 

相關途徑:

Route::post('/status/{statusId}/reply', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'status.reply', 
    'middleware' => ['auth'], 
]); 

Route::get('/user/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'profile.index', 
]); 

這是我StatusController裏面的方法,我用:

public function postReply(Request $request, $statusId) 
    { 
    $this->validate($request, [ 
     "reply-{$statusId}" => 'required|max:1000', 
    ], [ 
     'required' => 'You must have content to reply', 
    ]); 


    $status = Status::notReply()->find($statusId); 

    if(!$status) { 
     return redirect()->route('home'); 
    } 

    if(!Auth::user()->isFriendsWith($status->user) && Auth::user()->id !== $status->user->id) { 
     return redirect() 
     ->route('home') 
     ->with('info', 'Must be a friend to reply to this status, or be signed in'); 
    } 

     $reply = Status::create([ 
     'body' => $request->input("reply-{$statusId}"), 
     ])->user()->associate(Auth::user()); 

     $status->replies()->save($reply); 

     return redirect()->back()->with('info',"Reply has been comented!"); 


    } 

所有路線:

<?php 

/** 
* Home 
**/ 

Route::get('/', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'home', 
]); 


/* 
* Authentication 
*/ 

Route::get('/signup', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'auth.signup', 
    'middleware' => ['guest'], 
]); 

Route::post('/signup', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'middleware' => ['guest'], 
]); 

Route::get('/signin', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'auth.signin', 
    'middleware' => ['guest'], 
]); 

Route::post('/signin', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'middleware' => ['guest'], 
]); 

Route::get('/signout', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'auth.signout', 
]); 

/* 
* search 
*/ 

Route::get('/search', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'search.results', 
]); 

/* 
* Profiles 
*/ 

Route::get('/user/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'profile.index', 
]); 

Route::get('/profile/edit', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'profile.edit', 
    'middleware' => ['auth'], 
]); 

Route::post('/profile/edit', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'profile.edit', 
    'middleware' => ['auth'], 
]); 

/* 
* friends 
*/ 

Route::get('/friends', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'friends.index', 
    'middleware' => ['auth'], 
]); 

Route::get('/friends/add/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'friends.add', 
    'middleware' => ['auth'], 
]); 

Route::get('/friends/accept/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'friends.accept', 
    'middleware' => ['auth'], 
]); 

/* 
* Statuses 
*/ 

Route::post('/status', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'status.post', 
    'middleware' => ['auth'], 
]); 

Route::post('/status/{statusId}/reply', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'status.reply', 
    'middleware' => ['auth'], 
]); 
+0

如果您需要更多的代碼,請讓我知道你需要什麼=] – NewbieCoder

回答

0

不看代碼太多我以前見過的錯誤。但是,如果沒有給出答案,您可以顯示項目的所有路線。

錯誤通常是你有2條相同的路線。

/users/find 
/users/{username} 

而且您要發送的路由說,例如

/users/find 

但laravel認爲它被送到

/users/{username} 

而且沒有通過

+0

或許到那時[「用戶名」 => $ reply->用戶>用戶名])爲空。 – Jordan

+0

路線現在在那裏! – NewbieCoder

0
發送參數

laravel如何知道如何處理

Route::get('/user/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
     'as' => 'profile.index', 
]); 

你被引導到後URL解碼/用戶/ {用戶名}

看的文檔生成的URL命名路由 http://laravel.com/docs/5.1/routing

Route::get('user/{id}/profile',   ['as' => 'profile', function ($id) { 
    // 
}]); 

    $url = route('profile', ['id' => 1]); 

從文檔

的網址
+0

是的,我需要在這裏展示更多代碼!但是我發現了我會發布的問題,但是我留下了一些需要的代碼,謝謝您指出這一點! – NewbieCoder

0

我確實遺漏了很多需要的代碼,這些代碼確實有助於解釋一些事情,但我確實發現了這個問題。它的形式實際上高了一些。我發佈了表單的頂部,並將其保留爲不正確,以便我可以告訴你需要糾正的問題。

在窗體的頂部,我有兩個地方我試圖鏈接,所以當用戶單擊名稱或圖片或任何錨點時,它將路由到用戶的個人資料。在路由目錄中,它看起來像:

Route::get('/user/{username}', [ 
    'uses' => '\Chatty\Http\Controllers\[email protected]', 
    'as' => 'profile.index', 
]); 

所以這就是我通過URL〜/ user/firstuser獲取的內容。

但是下面這行代碼給了我一個〜/ user /%7Busername%7D的URL。原因是我沒有通過路由參數傳遞用戶名。爲了簡化這一點,只需將$ status-> user-> username作爲用戶名>用戶名即可。來自某些模型的關係,這就是爲什麼我有前面的$狀態。

因此,我們需要在這裏修復的是我稱之爲路徑()的地方,所有事情都需要在這裏進行。我很早就關閉了它。

href="{{ route('profile.index'), ['username' => $status->user->username] }}" 

這是正確的一行。

href="{{ route('profile.index', ['username' => $status->user->username]) }}" 
+0

顯然,我可以通過它的方法向控制器展示更多的內容,但我只想指出可能發生這種情況的其他人的主要問題。 – NewbieCoder

+0

很好的捕捉新手編碼器 – lagbox

相關問題