为什么我在URL末尾获得%7Busername%7D

时间:2015-12-22 00:18:43

标签: php laravel

好的,出于某些原因我得到:localhost / new_laravel_social_site / public / user /%7Busername%7D

我应该得到这个: 本地主机/ new_laravel_social_site /公共/用户/用户名

这里有很多代码,但我正在使用codecoarse laravel社交媒体网站。我点击时间线上的人,然后就出现了。但如果我点击他们的个人资料,那么它的工作正常。我会将代码发布到破坏的地方。

我正在打破{{$ reply-> user-> getNameOrUsername()}}。我有一切工作,这是与我使用$ reply的东西。

编辑:所有路线现在都在底部。

  @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\StatusController@postReply',
  'as' => 'status.reply',
  'middleware' => ['auth'],
]);

Route::get('/user/{username}', [
  'uses' => '\Chatty\Http\Controllers\ProfileController@getProfile',
  '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\HomeController@index',
  'as' => 'home',
]);


/*
* Authentication
*/

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

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

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

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

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

/*
* search
*/

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

/*
* Profiles
*/

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

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

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

/*
* friends
*/

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

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

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

/*
* Statuses
*/

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

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

4 个答案:

答案 0 :(得分:0)

在没有太多查看代码的情况下,我之前看到过这个错误。但是如果没有给出答案,你可以展示项目的所有路线。

错误通常是你有2条相同的路线。

Private Sub Form_Unload(Cancel As Integer)

Dim rs As DAO.Recordset
Dim currdate, check As String

Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Rate WHERE Property = " & Me.Property)
currdate = Date


'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
    'Check if rate is current rate
    If currdate >= [tbl_rate.Start_Date] And currdat <= [tbl_rate.End_Date] Then
        check = "Good"
    Exit Do
    Else
    'Move to the next record.
        check = "Bad"
        rs.MoveNext
    End If
Loop
MsgBox check 'testing to see if correctly identified if current rate exists
Else
MsgBox "There are no records in the recordset."
End If

If check = "Bad" Then
MsgBox "Please enter current rate."
Cancel = True
End If

rs.Close 'Close the recordset
Set rs = Nothing 'Clean up


End Sub

你正在发送路线说例如

/users/find
/users/{username}

但是laravel认为它被送到了

/users/find

你没有通过

发送参数

答案 1 :(得分:0)

laravel如何知道如何处理

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

网址解码后您定向到的网址是/ users / {username}

查看用于生成命名路由的URL的文档 https://developers.facebook.com/tools/debug/og/object/

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

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

来自文档

答案 2 :(得分:0)

我确实遗漏了许多所需的代码,这些代码真的有助于解释一些事情,但我确实发现了问题。实际上它在形式上略高一些。我发布了表单的顶部并将其保留为错误,因此我可以告诉您要更正的内容。

在表单的顶部我有两个地方,我试图链接这个,所以当用户点击名称或图片或任何锚点时,它将路由到用户的个人资料。在路径目录中,它看起来像:

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

所以这就是我通过URL〜/ user / firstuser。

但下面的这一行给了我一个〜/ user /%7Busername%7D的URL。原因是我没有通过route参数传递用户名。为了简化这一点,只需将$ status-&gt; user-&gt; username视为user-&gt; username。某些模型之间存在关系,这就是我在前面获得$ status的原因。

所以我们需要解决的问题是我在哪里调用route(),一切都需要进入。我早点关闭它。

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

这是正确的行。

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

答案 3 :(得分:0)

我有一个稍微不同的问题,但是也许有人会派上用场。

Route::patch('/margin/update/{{id}}', 'MarginsController@update')->name('margin.update');
  • 在视野中
action="{{ route('admin.margin.update',  $margin->id) }}"
    网址中的
  • 已收到链接

denis.loc / admin / margin / update / 4%7D <=即4%7D而不是(int)“ 4”

道德。请注意,必须有一个{id},但是我在Route中写了两个`{{id}}}。