Laravel分组路线问题

时间:2014-07-26 19:45:42

标签: laravel-4 controller routing

我尝试在Laravel中使用路由从前端分割后端。我想知道我应该如何对控制器进行分组

使用以下

// Get the URL segment to use for routing
$urlSegment = Config::get('feeds2go::app.access_url');

Route::group(array('prefix' => 'admin'), function() {
    //$urlSegment = Config::get('feeds2go::app.access_url_admin');
    Route::filter('adminFilter', 'Fefe\Feeds2go\Filters\Admin');
    Route::controller('/posts', 'Fefe\Feeds2go\Controllers\PostsController');
    Route::controller('', 'Fefe\Feeds2go\Controllers\DashController');
});


Route::controller($urlSegment, 'Fefe\Feeds2go\Controllers\SiteController');

/** Include IOC Bindings * */
include __DIR__ . '/bindings.php';

当我点击/admin时,正确的控制器被解雇但/admin/posts错误的控制器被调用SiteController而不是PostsController

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以具体了解路线并应用路线组,如下所示:

    // Admin routes
Route::group(array('before' => 'auth|admin'), function()
{
    Route::resource('admin/', 'AdminController@index');
    Route::get(//route details here
    //More specific routes here...
});

然后,在您的filters.php中,定义过滤器:

Route::filter('admin', function()
{
    // get current user
    $user = //code to get current user;

    // code to check user permissions, depending on how 
    // you've defined an admin user

    if ( ! adminuser )
    {
        return Redirect::home()
        ->with('flash_message', 'You do not have the appropriate priviliges to view the requested page.');
    }
});

或者,您可以使用基于模式的过滤器在文档中列出:enter link description here

基于模式的过滤器

您还可以指定过滤器根据其URI应用于整个路径集。

Route::filter('admin', function()
{
    //
});

Route::when('admin/*', 'admin');