【问题标题】:Laravel 5.2 Route ErrorLaravel 5.2 路由错误
【发布时间】:2016-02-13 04:25:57
【问题描述】:

在我的 home.blade.php 中有以下代码

<a href="{{ route('tasks.index') }}" class="btn btn-info">View Tasks</a>
<a href="{{ route('tasks.create') }}" class="btn btn-primary">Add New Task</a>

然后在 routes.php 我有以下内容,

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

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

Route::get('/create', [
    'as' => 'create',
    'uses' => 'TasksController@create'
]);

为什么http://localhost:8000/ 出现此错误

路线 [tasks.index] 未定义。 (查看:D:\wamp\www\test1\resources\views\pages\home.blade.php)

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    错误

    路线 [tasks.index] 未定义。 (看法: D:\wamp\www\test1\resources\views\pages\home.blade.php)

    这是因为您将其命名为index 而不是tasks.index,所以要么在路由声明中将名称从index 更改为task.index,要么在href 属性中引用路由时使用index。现在你有了这个:

    Route::get('/index', [
        'as' => 'index', // index is the name here so use the name as it is
        'uses' => 'TasksController@index'
    ]);
    

    tasks.create 也一样:

    Route::get('/create', [
        'as' => 'create', // Name is "create" not "tasks.create"
        'uses' => 'TasksController@create'
    ]);
    

    如果你使用一个组来命名会更好(对于 V-5.1 和更高版本):

    Route::group(['as' => 'tasks.'], function () {
    
        Route::get('/index', [
            'as' => 'index', // Now you can usee 'tasks.index'
            'uses' => 'TasksController@index'
        ]);
    
        Route::get('/create', [
            'as' => 'create', // Now you can usee 'tasks.create'
            'uses' => 'TasksController@create'
        ]);
    });
    

    【讨论】:

      【解决方案2】:

      错误是因为 Laravel 找不到任何名为 tasks.indextasks.create 的路由。这是因为您将路线命名为 indexcreatehome

      因此,如果您希望链接指向 URL:/tasks,则必须使用其名称链接到该路由。

      即:网址将是route('index')。 这是取自路线:

      routes.php 文件中可以看到,'as'=&gt;'index' 是路线的名称,这是您应该调用的名称。

      所以链接变成:

      <a href="{{ route('index') }}" class="btn btn-info">View Tasks</a>
      <a href="{{ route('create') }}" class="btn btn-info">CreateTasks</a>
      

      【讨论】:

        【解决方案3】:

        正如 Alpha 所说,最好将路线分组。你也可以像这样链接方法

            Route::group(['as' => 'tasks.'], function () 
        
            {
        
            Route::get('/index', 'TasksController@index')->name(index);
        
            Route::get('/create', 'TasksController@create')->name(create);
        
            });
        

        定义路由后就可以使用路由功能了

        {{ route('tasks.index') }}
        {{ route('tasks.create') }}
        

        或者,如果您不想对路线进行分组,您可以这样做:

        Route::get('/index', 'TasksController@index')->name(tasks.index);
        
        Route::get('/create', 'TasksController@create')->name(tasks.create);
        

        现在你可以使用了:

        <a href="{{ route('tasks.index') }}" class="btn btn-info">View Tasks</a>
        <a href="{{ route('tasks.create') }}" class="btn btn-primary">Add New Task</a>
        

        您可以在项目文件夹中查看您拥有哪些路由及其运行此命令的名称:

        php artisan route:list
        

        【讨论】:

          猜你喜欢
          • 2016-04-25
          • 2016-07-03
          • 2016-08-30
          • 2016-12-01
          • 2016-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多