Laravel RESTful控制器路由

时间:2014-01-06 18:13:12

标签: routing laravel-4

我正在尝试访问我的网址:

  

www.mysite.com/user/dash/sales

我的控制器目录中有一个DashboardController.php文件:

<?php

class DashboardController extends BaseController {

    public function __construct() {

        $this->beforeFilter('auth');

    }

    /**
     * Supplier's dashboard screen
     *
     */
    public function getSupplier()
    {
        $this->layout->content = View::make('user.dashboard.supplier');
    }

    /**
     * Sales dashboard screen
     *
     */
    public function getSales()
    {
        $this->layout->content = View::make('user.dashboard.sales');
    }

    /**
     * Admin's dashboard screen
     *
     */
    public function getAdmin()
    {
        $this->layout->content = View::make('user.dashboard.admin');
    }

}

我在routes.php文件中尝试了以下所有可能,但没有运气:

Route::any('user/dash/(:any)', array('uses' => 'DashboardController') );

Route::controller( 'user/dash', 'DashboardController' );

Route::group(array('prefix' => 'user', 'before' => 'auth'), function() 
{ 
    Route::controller('dash', 'DashboardController');
});

有没有人有任何其他建议?我不太清楚如何使这条路成功。我得到的所有这些路线的错误信息是:

  

找不到控制器方法。

1 个答案:

答案 0 :(得分:3)

好了经过多次挖掘并阅读大量文章后,有一条名为“先进先出”的规则:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

/** RESTful Controllers **/
Route::controller( 'user/dash', 'DashboardController' );
Route::controller( 'user', 'UserController' );
Route::controller( 'product', 'ProductController' );

Route::group(array('prefix' => 'dash', 'before' => 'auth'), function()
{
    Route::controller('product', 'Dash_ProductController');
    Route::controller('user', 'Dash_UserController');
});

/** Home/Fallback Controller **/
Route::controller('/', 'HomeController');

因此,如果你有一条通向用户的路线,但是想要更深入,你必须在routes.php文件中加入最深的FIRST!

很棒的文章:http://laravel.io/topic/30/routes-first-in-first-out

回答者:user3130415