php - 为什么我的 URL 末尾会出现 %7Busername%7D

标签 php laravel

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

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

这方面有很多代码,但我正在做 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

模型

统计.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');
  }

}

用户.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'],
]);

最佳答案

对于 googlers ...在我的例子中是在我的路线结束时额外的 }....

Route::get('url/{uuid}}', function ($uuid) {

代替

Route::get('url/{uuid}', function ($uuid) {

关于php - 为什么我的 URL 末尾会出现 %7Busername%7D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34406578/

相关文章:

php - 在 PHP 中使用 MySQLi 准备语句,得到 "No data supplied for parameters in prepared statement "

php - 从它的零项微移一个数组

PHP 购物篮问题

laravel - 从数据透视表 laravel 中删除一个条目

php - 如何使用 anchor 标记显示 Laravel 验证错误

php - jquery打字效果

javascript - 第一张图片在第二张图片上传时保存?

Laravel 在类里面的责任

php - 主管 laravel 不起作用并启动

twitter-bootstrap - Modal 中的 Vue.js AJAX 调用