Laravel 5.2缺少[Route:user.profile] [URI:user / {nickname} / profile]

时间:2016-02-07 22:29:48

标签: php laravel laravel-routing laravel-5.2

我一直收到此错误

ErrorException in UrlGenerationException.php line 17:

当任何页面加载并且我已登录时。

这是我的导航仪的样子

@if(Auth::guest())
                <li><a href="{{ url('/login') }}">Log In</a></li>
                <li><a href="{{ url('/register') }}">Sign Up</a></li>
            @else
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ Auth::user()->nickname }}<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="{{ route('user.profile') }}">Profile</a></li>
                        <li><a href="{{ route('user.settings') }}">Settings</a></li>
                        <li><a href="{{ url('/logout') }}">Log Out</a></li>
                    </ul>
                </li>
            @endif

我遇到的问题是{{route('user.profile')}}无效?

当我点击链接是www.mydomain.com/User/SCRATK/profile工作正常但页面不会加载因为这个错误??

Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile].

这是我的路线档案

Route::group(['middleware' => 'web'], function () {
Route::auth();

Route::get('/', ['as' => 'home', 'uses' => 'BaseController@index']);

Route::group(['namespace' => 'User', 'prefix' => 'user'], function(){
    Route::get('{nickname}/settings', ['as' => 'user.settings', 'uses' => 'SettingsController@index']);
    Route::get('{nickname}/profile', ['as' => 'user.profile', 'uses' => 'ProfileController@index']);
});
});

3 个答案:

答案 0 :(得分:37)

您必须将路由参数传递给route方法,例如:

<li><a href="{{ route('user.profile', $nickname) }}">Profile</a></li>
<li><a href="{{ route('user.settings', $nickname) }}">Settings</a></li>

这是因为,两条路线在路线声明中都有{nickname}。我已使用$nickname作为示例,但请确保将$nickname更改为适当的值/变量,例如,它可能类似于以下内容:

<li><a href="{{ route('user.settings', auth()->user()->nickname) }}">Settings</a></li>

答案 1 :(得分:2)

我在laravel 5.2中的解决方案

{{ Form::open(['route' => ['votes.submit', $video->id],  'method' => 'POST']) }}
    <button type="submit" class="btn btn-primary">
        <span class="glyphicon glyphicon-thumbs-up"></span> Votar
    </button>
{{ Form::close() }}

我的路线文件(在中间件下)

Route::post('votar/{id}', [
    'as' => 'votes.submit',
    'uses' => 'VotesController@submit'
]);

Route::delete('votar/{id}', [
    'as' => 'votes.destroy',
    'uses' => 'VotesController@destroy'
]);

答案 2 :(得分:-2)

Route::group(['middleware' => 'web'], function () {
Route::auth();

Route::get('/', ['as' => 'home', 'uses' => 'BaseController@index']);

Route::group(['namespace' => 'User', 'prefix' => 'user'], function(){
    Route::get('{nickname}/settings', ['as' => 'user.settings', 'uses' => 'SettingsController@index']);
    Route::get('{nickname}/profile', ['as' => 'user.profile', 'uses' => 'ProfileController@index']);
});
});