Laravel控制器到控制器

时间:2019-03-27 19:11:24

标签: php laravel

我已经使用Laravel建立了一个我很满意的网站,但是由于没有提前计划,我已经建立了管理/管理面板,现在我必须仔细研究一下用户前端,我想知道我应该如何处理这个问题。

目前,我有一个 web.php 文件,其中包含以下内容:

# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController@index');
Route::get('/admin/wedding/create', 'WeddingController@create');
Route::get('/admin/wedding/{wedding}', 'WeddingController@show');
Route::get('/admin/wedding/{wedding}/edit', 'WeddingController@edit');

Route::post('/admin/wedding/create', 'WeddingController@store');
Route::put('/admin/wedding/{wedding}/edit', 'WeddingController@update');
Route::delete('/admin/wedding/{wedding}/destroy', 'WeddingController@destroy');

# MENU HANDLING
Route::get('/admin/wedding/{wedding}/menus', 'MenuController@index');
Route::get('/admin/wedding/{wedding}/menu/create', 'MenuController@create');
Route::get('/admin/wedding/{wedding}/menu/{menu}', 'MenuController@show');
Route::get('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController@edit');

Route::post('/admin/wedding/{wedding}/menu/create', 'MenuController@store');
Route::put('/admin/wedding/{wedding}/menu/{menu}/edit', 'MenuController@update');
Route::delete('/admin/wedding/{wedding}/menu/{menu}/delete', 'MenuController@destroy');
...continues

因此,您可能会看到我有多个控制器为管理员处理此操作,但是,如果用户登录,他们应该能够查看婚礼上的菜单,我可以将其与 @auth 标记,然后像这样将其分开,但是我恐怕最终会得到一个如下所示的 web.php

# WEDDING HANDLING
Route::get('/admin/weddings', 'WeddingController@index');
Route::get('/weddings', 'WeddingController@index');
...continues

希望从我的演示中可以看到我的要求。

tl; dr::如何在不重复大量代码的情况下分隔用户/管理区域。

4 个答案:

答案 0 :(得分:3)

您只需要创建不同的控制器来处理前端,例如:FrontEndMenuController @ yourFunction

您还可以将管理路由分成带有前缀“ admin”的组,而不是在所有路由上都重复:

Route::prefix('admin')->group(function () {
   Route::get('/wedding/{wedding}/menus', 'MenuController@index');
   Route::get('/wedding/{wedding}/menu/create', 'MenuController@create');
   Route::get('/wedding/{wedding}/menu/{menu}', 'MenuController@show');
   Route::get('/wedding/{wedding}/menu/{menu}/edit', 'MenuController@edit');
});

然后使用这样的前端路由:

Route::get('/wedding/{wedding}/menu', 'FrontEndMenuController@index');

答案 1 :(得分:2)

您可以创建一个新的FrontEndController,并将任何前端页面路由到该控制器。

Route::get('/admin/weddings', 'WeddingController@index');
Route::get('/weddings', 'FrontEndControllerController@showWeddings');

然后,您可以使用中间件来区分和重复任何重复的代码到Wedding模型中,然后调用它。

答案 2 :(得分:2)

由于您几乎要遵守 CRUD / REST约定,因此 Adriano Marra 的答案中的另一项就是也要使用资源控制器

从Laravel documentation about resource controllers

  

Laravel资源路由通过单行代码将典型的“ CRUD”路由分配给控制器。


介绍资源控制器

在您的用例中,您的资源婚礼,因此您可以将 web.php 路由重写为:

Route::prefix('admin')->group(function () {
    Route::resource('weddings', 'WeddingsController');
});

这些行将注册这样的路由:

+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method    | URI                           | Name             | Action                                          | Middleware |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+
|        | GET|HEAD  | admin/weddings                | weddings.index   | App\Http\Controllers\WeddingsController@index   | web        |
|        | POST      | admin/weddings                | weddings.store   | App\Http\Controllers\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/create         | weddings.create  | App\Http\Controllers\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}      | weddings.show    | App\Http\Controllers\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}      | weddings.update  | App\Http\Controllers\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}      | weddings.destroy | App\Http\Controllers\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/edit | weddings.edit    | App\Http\Controllers\WeddingsController@edit    | web        |
+--------+-----------+-------------------------------+------------------+-------------------------------------------------+------------+

注意:的差别很小,因为Route::resource(...)方法将您提供给它的所有七种路由的复数资源名称(weddings)都使用单数形式参数名称(wedding)。

然后您可以使用以下方式注册菜单资源:

Route::prefix('admin')->group(function () {
    Route::resource('weddings', 'WeddingsController');

    // Personally, I would make 'menus' a top level resource but I will
    // stick to your routing example for the rest of the answer.
    Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});

命名组资源路由

此外,您还可以将前端婚礼控制器注册为资源

Route::prefix('admin')->group(function () {
    Route::resource('weddings', 'WeddingsController');
    Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});

// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);

您的应用程序注册的路由为:

+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method    | URI                                        | Name             | Action                                          | Middleware |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+
|        | GET|HEAD  | admin/weddings                             | weddings.index   | App\Http\Controllers\WeddingsController@index   | web        |
|        | POST      | admin/weddings                             | weddings.store   | App\Http\Controllers\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/create                      | weddings.create  | App\Http\Controllers\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}                   | weddings.show    | App\Http\Controllers\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}                   | weddings.update  | App\Http\Controllers\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}                   | weddings.destroy | App\Http\Controllers\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/edit              | weddings.edit    | App\Http\Controllers\WeddingsController@edit    | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus             | menus.index      | App\Http\Controllers\WeddingsController@index   | web        |
|        | POST      | admin/weddings/{wedding}/menus             | menus.store      | App\Http\Controllers\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/create      | menus.create     | App\Http\Controllers\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}      | menus.show       | App\Http\Controllers\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}/menus/{menu}      | menus.update     | App\Http\Controllers\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}/menus/{menu}      | menus.destroy    | App\Http\Controllers\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}/edit | menus.edit       | App\Http\Controllers\WeddingsController@edit    | web        |
|        | GET|HEAD  | weddings                                   | weddings.index   | App\Http\Controllers\FrontendController@index   | web        |
|        | GET|HEAD  | weddings/{wedding}                         | weddings.show    | App\Http\Controllers\FrontendController@show    | web        |
+--------+-----------+--------------------------------------------+------------------+-------------------------------------------------+------------+

如果您仔细查看上表,您会注意到name列具有一些重复的值。当您需要在应用程序中的任何位置按名称引用这些特定路由时,这会造成冲突。

因此,您可以通过在管理组的命名路由前添加自定义前缀来解决此问题:

Route::prefix('admin')->name('admin.')->group(function () {
    Route::resource('weddings', 'WeddingsController');
    Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});

// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);

这将解决任何路由冲突,因为管理路由已正确地以admin.名称为前缀

+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
| Domain | Method    | URI                                        | Name                   | Action                                          | Middleware |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+
|        | GET|HEAD  | admin/weddings                             | admin.weddings.index   | App\Http\Controllers\WeddingsController@index   | web        |
|        | POST      | admin/weddings                             | admin.weddings.store   | App\Http\Controllers\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/create                      | admin.weddings.create  | App\Http\Controllers\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}                   | admin.weddings.show    | App\Http\Controllers\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}                   | admin.weddings.update  | App\Http\Controllers\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}                   | admin.weddings.destroy | App\Http\Controllers\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/edit              | admin.weddings.edit    | App\Http\Controllers\WeddingsController@edit    | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus             | admin.menus.index      | App\Http\Controllers\WeddingsController@index   | web        |
|        | POST      | admin/weddings/{wedding}/menus             | admin.menus.store      | App\Http\Controllers\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/create      | admin.menus.create     | App\Http\Controllers\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}      | admin.menus.show       | App\Http\Controllers\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}/menus/{menu}      | admin.menus.update     | App\Http\Controllers\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}/menus/{menu}      | admin.menus.destroy    | App\Http\Controllers\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit       | App\Http\Controllers\WeddingsController@edit    | web        |
|        | GET|HEAD  | weddings                                   | weddings.index         | App\Http\Controllers\FrontendController@index   | web        |
|        | GET|HEAD  | weddings/{wedding}                         | weddings.show          | App\Http\Controllers\FrontendController@show    | web        |
+--------+-----------+--------------------------------------------+------------------------+-------------------------------------------------+------------+

命名空间组

最后,您可以通过引入名称空间来进一步优化(如果您开始拥有越来越多的控制器,则很有用)。

您可以在app/Http/Controllers中创建一个文件夹,例如Administration,在其中存储所有管理控制器。

web.php 文件中,您只需要告诉Laravel,以管理员为前缀的路由组应在新创建的文件夹中查找控制器:

Route::prefix('admin')->namespace('Administration')->name('admin.')->group(function () {
    Route::resource('weddings', 'WeddingsController');
    Route::resource('weddings/{wedding}/menus', 'WeddingMenusController');
});

// This will map only two methods for this resource.
Route::resource('weddings', 'FrontendController')->only(['index', 'show']);

这将导致注册以下路线:

+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
| Domain | Method    | URI                                        | Name                   | Action                                                         | Middleware |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+
|        | GET|HEAD  | admin/weddings                             | admin.weddings.index   | App\Http\Controllers\Administration\WeddingsController@index   | web        |
|        | POST      | admin/weddings                             | admin.weddings.store   | App\Http\Controllers\Administration\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/create                      | admin.weddings.create  | App\Http\Controllers\Administration\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}                   | admin.weddings.show    | App\Http\Controllers\Administration\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}                   | admin.weddings.update  | App\Http\Controllers\Administration\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}                   | admin.weddings.destroy | App\Http\Controllers\Administration\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/edit              | admin.weddings.edit    | App\Http\Controllers\Administration\WeddingsController@edit    | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus             | admin.menus.index      | App\Http\Controllers\Administration\WeddingsController@index   | web        |
|        | POST      | admin/weddings/{wedding}/menus             | admin.menus.store      | App\Http\Controllers\Administration\WeddingsController@store   | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/create      | admin.menus.create     | App\Http\Controllers\Administration\WeddingsController@create  | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}      | admin.menus.show       | App\Http\Controllers\Administration\WeddingsController@show    | web        |
|        | PUT|PATCH | admin/weddings/{wedding}/menus/{menu}      | admin.menus.update     | App\Http\Controllers\Administration\WeddingsController@update  | web        |
|        | DELETE    | admin/weddings/{wedding}/menus/{menu}      | admin.menus.destroy    | App\Http\Controllers\Administration\WeddingsController@destroy | web        |
|        | GET|HEAD  | admin/weddings/{wedding}/menus/{menu}/edit | admin.menus.edit       | App\Http\Controllers\Administration\WeddingsController@edit    | web        |
|        | GET|HEAD  | weddings                                   | weddings.index         | App\Http\Controllers\FrontendController@index                  | web        |
|        | GET|HEAD  | weddings/{wedding}                         | weddings.show          | App\Http\Controllers\FrontendController@show                   | web        |
+--------+-----------+--------------------------------------------+------------------------+----------------------------------------------------------------+------------+

答案 3 :(得分:0)

这是我需要分开管理路线和前端路线时要做的事情

Route::group([
        'prefix'          => 'admin',
        'namespace'       => 'Admin', // assumed
        // 'as'           => 'admin.'  
        // 'middleware'   => 'admin.'  
    ], function () {

        Route::get('weddings', 'WeddingController@index');

        Route::group([
            'prefix'       => 'wedding',
        ], function () {
            // Wedding Management
            Route::get('{wedding}/show', 'WeddingController@show'); // Updated with /show in url otherwise it will overlap create

            Route::get('{wedding}/edit', 'WeddingController@edit');
            Route::put('{wedding}/edit', 'WeddingController@update');

            Route::delete('{wedding}/destroy', 'WeddingController@destroy');

            Route::get('create', 'WeddingController@create');
            Route::post('create', 'WeddingController@store');

            // Here you differentiate WeddingController and MenuController

            // Route::group([
            //    'middleware'       => 'only_applied_to_this_group',
            // ], function () {
                    // Menu Management
                    Route::get('{wedding}/menus', 'MenuController@index');
                    Route::get('{wedding}/menu/create', 'MenuController@create');
                    Route::get('{wedding}/menu/{menu}', 'MenuController@show');
                    Route::get('{wedding}/menu/{menu}/edit', 'MenuController@edit');

                    Route::post('{wedding}/menu/create', 'MenuController@store');
                    Route::put('{wedding}/menu/{menu}/edit', 'MenuController@update');
                    Route::delete('{wedding}/menu/{menu}/delete', 'MenuController@destroy')
            // });
        });
    });

    Route::group([
        'namespace'    => 'Front',
        'as'           => 'front.'
    ], function () {
        // Front-end Management
    });

我希望将其简化以更好地理解路线,希望如此。