Laravel:路线 [users.edit] 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34494263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:54:56  来源:igfitidea点击:

Laravel: Route [users.edit] not defined

laravellaravel-4laravel-5laravel-routing

提问by Niladri Banerjee - Uttarpara

I have created a demo application in Laravel 4, for some learning experience. The routes.php is as follows:

我在 Laravel 4 中创建了一个演示应用程序,以获取一些学习经验。route.php 如下:

Route::get('/', function()
{
    return View::make('hello');
});

Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
    $layout = View::make('users.dashboard');
    $layout->title = "Dashboard";
    $layout->content = View::make('users.dashboard');       
    return $layout;
}); 
Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController@all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController@create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController@getLogout']);
Route::resource('/','UserController');
});

Route::controller('users', 'UserController');

Here, I prefixed the "users". However, Trying logging in with/out correct login details, it shows only the login page instead of showing up the error message on the login page or dashboard.

在这里,我在“用户”前面加上了前缀。但是,尝试使用/退出正确的登录详细信息登录时,它仅显示登录页面,而不是在登录页面或仪表板上显示错误消息。

Any suggestion? FYI,

有什么建议吗?供参考,

The index.blade.php in app\views\users is below:

app\views\users 中的 index.blade.php 如下:

@extends('users.user') 
@section('title', 'User Listing Page')
@section('description', 'This is the user listing page')
@section('main')

<h1>All Users</h1>

<p>{{ link_to_route('users.create', 'Add new user') }}</p>

<h1>Login</h1>
<p>{{ link_to_route('users.login', 'Login') }}</p> 

<table>

    <tr>

        <td>{{ $users->links() }}</td>

    </tr>       

</table>


@if ($users->count())
<table class="table table-striped table-bordered">
    <thead>
        <tr>
    <th>Username</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Name</th>
        </tr>
    </thead>

    <tbody>
        @foreach ($users as $user)
            <tr>
      <td>{{ $user->username }}</td>
      <td>{{ $user->email }}</td>
      <td>{{ $user->phone }}</td>
      <td>{{ $user->name }}</td>
      <td>{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}</td>
      <td>{{ Form::open(array('method' => 'DELETE', 'route' => array('users.destroy', $user->id))) }}
      {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
                    {{ Form::close() }}
                </td>
            </tr>
        @endforeach



    </tbody>

</table>

<table>

    <tr>

        <td>{{ $users->links() }}</td>

    </tr>       

</table>

@else
    There are no users
@endif

@stop

The script login.blade.php (on the same location as above) as follows:

脚本 login.blade.php(在与上面相同的位置)如下:

@extends('users.user') 

@section('main')

<h1>Login</h1>

{{ Form::open(array('route' => 'users.login')) }}

<ul>


    <li>
        {{ Form::label('username', 'Username:') }}
        {{ Form::text('username') }}
    </li>

    <li>
        {{ Form::label('password', 'Password:') }}
        {{ Form::password('password') }}
    </li>


    <li>
        {{ Form::submit('Submit', array('class' => 'btn')) }}
    </li>
</ul>
{{ Form::close() }}

@if ($errors->any())
<ul>
    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif

@stop

回答by Saiyan Prince

There is no users.editroute defined in your routesfile. Hence, you are bound to receive that error. There's no users.edit route in your code.

users.edit您的routes文件中没有定义路由。因此,您一定会收到该错误。您的代码中没有 users.edit 路由。

Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
    Route::get('dashboard', function () {
        $layout = View::make('users.dashboard');
        $layout->title = "Dashboard";
        $layout->content = View::make('users.dashboard');       
        return $layout;
    }); 
    Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController@all']);
    Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController@create']);
    Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@getLogin']);
    Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController@postSignin']);
    Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController@getLogout']);
    Route::resource('/','UserController');
});

Add the following route somewhere in your routes file and the error should disappear:

在路由文件中的某处添加以下路由,错误应该会消失:

Route::get('/edit/', [ 'as' => 'users.edit', 'uses' => 'UserController@edit']);

Note: You need to replace the controller method name editto whatever you have defined.

注意:您需要将控制器方法名称替换为edit您定义的任何名称。



UPDATE 1

更新 1

That is because, you have 'before' => 'auth'parameter passed in your Route::group()method. Just remove usersfrom editroute and I hope that should do the job for you.

那是因为,您'before' => 'auth'Route::group()方法中传递了参数。只需usersedit路线中移除,我希望这能为您完成这项工作。

Just A Side note

只是一个旁注

I presume that you are new in laravel and have less amount of experience with the framework. I would recommend you to go with this only in order to get your hands dirty with the basics. Then once you start gaining the experience and know how things work behind the scenes, you can jump off to the next level.

我认为您是 laravel 的新手并且对该框架的经验较少。我建议您仅使用此方法,以便了解基础知识。然后,一旦您开始获得经验并了解幕后的运作方式,您就可以跳到下一个层次。

If I were at your place, I wouldn't have gone with the code that you have provided, instead I would have kept it way too simple for now. My routes.phpfile would be like this:

如果我是你的地方,我就不会去与您所提供的代码,而不是我会保持它的方式太简单了现在。我的routes.php文件是这样的:

/* Routes for users not logged in. */
Route::get('/all', 'UserController@all');
Route::get('/create', 'UserController@create');
Route::get('users/login', 'UserController@getLogin');
Route::post('users/login', 'UserController@postSignin');
Route::get('users/logout', 'UserController@getLogout');

/* Routes for logged in users */
Route::get('/users/dashboard', 'UserSessionsController@dashboard');
Route::get('/users/edit/{id}', 'UserSessionsController@edit');

And then in your view file:

然后在您的视图文件中:

<a href="{{ url('/users/edit', $user->id) }}">EDIT</a>

Then I would have used the middleware only inside the UserSessionsControllerfile like this:

然后我会只在UserSessionsController文件内部使用中间件,如下所示:

/**
 * Check if the user is authenticated.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth'); // or any other middleware you have.
}


UPDATE 2:

更新 2:

Learn more about the middleware from the following links:

从以下链接了解有关中间件的更多信息:

For version: 5

对于版本:5

  1. Official Docs
  2. Matt Stauffer On Middleware
  1. 官方文档
  2. Matt Stauffer 谈中间件

For version: 5.1

对于版本:5.1

  1. Official Docs
  2. Youtube Video
  1. 官方文档
  2. 优酷视频

For version: 5.2

对于版本:5.2

  1. Official Docs
  1. 官方文档

Hope this helps you out. Happy Coding. Cheers.

希望这可以帮助你。快乐编码。干杯。