2015-12-22 693 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

相关问题