Laravel RESTful 控制器路由

6

我正在尝试访问以下URL:

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个回答

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文件中的最前面!阅读这篇精彩的文章:http://laravel.io/topic/30/routes-first-in-first-out。答案提供者:user3130415。

@kJamesy 这是存档的链接,http://web.archive.org/web/20130912011338/http://laravel.io/topic/30/routes-first-in-first-out - Stranger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接