在 Laravel 4 中创建管理员界面的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23295463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:25:14  来源:igfitidea点击:

Best practice for creating administrator interface in Laravel 4

phplaraveladministration

提问by Nagy Ervin

I would like to create an administrator interface for my Laravel project, which is completely separated from the user side.

我想为我的 Laravel 项目创建一个与用户端完全分离的管理员界面。

For example, in Yii framework I can make a module and this will ensure full separation from the user side. Inside a module I can use separate folder structure etc.

例如,在 Yii 框架中,我可以制作一个模块,这将确保与用户端完全分离。在模块内部,我可以使用单独的文件夹结构等。

回答by The Alpha

This is really a broad question and one answer can't cover everything about best practice for admin controllers or back end management but there are some basic concepts for building an Admin Panel:

这确实是一个广泛的问题,一个答案无法涵盖有关管理员控制器或后端管理最佳实践的所有内容,但有一些基本概念可用于构建Admin Panel

// Keep all of your admin routes inside something like this
Route::group(array('prefix'=> 'admin', 'before' => 'auth.admin'), function() {

    // Show Dashboard (url: http://yoursite.com/admin)
    Route::get('/', array('uses' => 'Admin\DashBoardController@index', 'as' => 'admin.home'));

    // Resource Controller for user management, nested so it needs to be relative
    Route::resource('users', 'Admin\UserController');

});

// Other routes (Non-Admin)
Route::get('login', array('uses' => 'AuthController@showLogin' 'as' => 'login'));

By using a prefixyou may separate all admin routes whose urlwill be prefixed with adminso, if you have a userscontroller for user management in back end then it's urlwill be prefixed with admin, i.e. site.com/admin/users. Also using a beforefilter you may add an authentication for all admin controllers in one place, that means, to access all of your admin controllers user must be logged in and the filter could be something like this:

通过使用 a,prefix您可以分隔所有urladminso为前缀的管理路由,如果您users在后端有一个用于用户管理的控制器,那么它将urladmin, 即为前缀site.com/admin/users。同样使用before过滤器,您可以在一个地方为所有管理控制器添加身份验证,这意味着要访问所有管理控制器,用户必须登录,过滤器可能是这样的:

Route::filter('auth.admin', function($route, $request, $args){
    // Check if the user is logged in, if not redirect to login url
    if (Auth::guest()) return Redirect::guest('login');

    // Check user type admin/general etc
    if (Auth::user()->type != 'admin') return Redirect::to('/'); // home
});

For, CRUD (Create, Read, Update, Delete) use a resourceful controller, for example, the UserControllerin an example of resourceful route declaration.

对于,CRUD(创建、读取、更新、删除)使用资源丰富的控制器UserController例如在资源丰富的路由声明示例中。

Use repository classes (Repository Pattern) for decoupling of dependencies, read this article.

使用存储库类(Repository Pattern)进行依赖解耦,阅读这篇文章

Always use a named route, i.e. array('as' => 'routename', 'uses' => 'SomeController@method'), this is an example of naming a route. Named routes are easy to refer, i.e. return Redirect::route('admin.home')will redirect to site.com/adminbecause we have used admin.homein asto assign the name for that route.

始终使用命名路由,即array('as' => 'routename', 'uses' => 'SomeController@method'),这是命名路由的示例。命名路由很容易引用,即return Redirect::route('admin.home')会重定向到,site.com/admin因为我们已经使用admin.homeinas为该路由分配名称。

Keep admin controllers in a separate folder and use a namespacefor example, Admin\\DashBoardController@indexcontroller should be in app/controllers/adminand your DashBoardControllercontroller should look like this:

将管理控制器放在一个单独的文件夹中并使用namespace例如,Admin\\DashBoardController@index控制器应该在app/controllers/admin,你的DashBoardController控制器应该是这样的:

<?php namespace Admin;

class DashBoardController extends \BaseController {
    public function index()
    {
        //...
    }
}

There are more but it's enough to start with, read articles online and must read the documentation.

还有更多,但足以开始,在线阅读文章并且必须阅读文档

回答by azngunit81

If you are familiar with composer you can import in packages (aka modules)

如果您熟悉作曲家,您可以导入包(又名模块)

There is a widely available module with multi level interface already called Sentry 2.0: https://github.com/cartalyst/sentry

有一个广泛可用的多级接口模块,已经称为 Sentry 2.0:https: //github.com/cartalyst/sentry

You could also make your own if needed if the one I propose is too complex.

如果我建议的太复杂,您也可以根据需要制作自己的。

There is even a "laravel-ready" version of sentry.

甚至还有一个“laravel-ready”版本的哨兵。

回答by Keith Mifsud

I use the same directory structure that you would like to use on most (if not all) my Laravel projects. Basically, I keep admin views and admin controllers separate from the front-end ones.

我使用的目录结构与您想在大多数(如果不是全部)我的 Laravel 项目中使用的目录结构相同。基本上,我将管理视图和管理控制器与前端视图分开。

Examples: Controllers:

示例: 控制器:

app/controllers/admin/Admin*Name*Controller.php app/controllers/site/*Name*Controller.php

app/controllers/admin/Admin*Name*Controller.php app/controllers/site/*Name*Controller.php

Views: app/views/admin/some_folder/index.blade.php app/views/site/some_folder/index.blade.php

浏览次数:应用程序/视图/管理/ some_folder/index.blade.php应用程序/视图/网站/ some_folder/index.blade.php

I would also suggest that you install this laravel project https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Sitewhich will give a very good starting on how to organise things in your laravel project. It also has the same folder structure you would like to use.

我还建议您安装这个 Laravel 项目https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site,这将为您在 Laravel 项目中如何组织事物提供一个很好的开端。它还具有您想要使用的相同文件夹结构。

Good luck.

祝你好运。