简体   繁体   English

Laravel中的命名空间控制器和路由

[英]Namespaced controllers and routes in laravel

Assume, I have a model "Post" and I create two resource controllers for it - User/PostController and Admin/PostController. 假设我有一个模型“ Post”,并为其创建了两个资源控制器-User / PostController和Admin / PostController。

So when I wish to consume the resource, my routes would look something like this: 因此,当我希望消耗资源时,我的路线将如下所示:

/user/post/:id
/admin/post/:id

Is this correct according to the convention or am I doing it wrong? 按照惯例,这是正确的还是我做错了?

It is recommended to use a separate model for its controller and vice versa. 建议为其控制器使用单独的模型,反之亦然。 This will allow you to maintain consistency and readability in the application, keeping the application easy to development. 这将使您能够保持应用程序的一致性和可读性,从而使应用程序易于开发。

In the application where there are admin and user accounts, I suggest using Middleware + ( roles + permissions ) and do not stand out in routing /user/ and /admin/ a apply general /user/ 在有管理员和用户帐户的应用程序中,我建议使用Middleware +(( roles + permissions )),不要在路由/user//admin/应用通用/user/

In the my example I described, you can use one method in the PostController , for example: show , add permission (eg user-post-list , admin ), add roles (eg admin , user ), in the routing describe who has permission (eg user-post-list , admin ) to given function (show) and assign: permissions to role, roles to user - one or many role to user. 在我描述的示例中,您可以在PostController使用一种方法,例如: show ,add权限(例如user-post-listadmin ),add角色(例如adminuser ),在路由中描述谁拥有权限(例如user-post-listadmin )分配给给定的功能(显示)并分配:角色权限,用户角色-用户一个或多个角色。 You can also add roles to the routing and assigning the appropriate permissions in controller. 您也可以在路由中添加角色,并在控制器中分配适当的权限。 You can load permissions from the file using fixtures , for example permissions.csv , roles.csv , role_permission.csv or use seeds and fixtures or only seeds - before make create roles and permissions - create the appropriate migrations) 您可以使用从文件加载权限fixtures ,例如permissions.csvroles.csvrole_permission.csv或用seedsfixtures或仅seeds -前使创建角色和权限-创建适当的迁移)

Second solution (and only using Seed) it not flexible, because enlarging the list of permissions and roles involves modifying the code as opposed to the first solution - loading them from the file. 第二种解决方案(并且仅使用Seed)并不灵活,因为与第一种解决方案相比,扩大权限和角色列表涉及修改代码-从文件中加载它们。

Of course, there are other solutions. 当然,还有其他解决方案。 I think that the proposing by me has a simple logic, it is effective and flexible to changes. 我认为我的提议有一个简单的逻辑,它是有效且灵活的变更。

Check this: https://itsolutionstuff.com/post/laravel-56-user-roles-and-permissions-acl-using-spatie-tutorialexample.html 检查此内容: https : //itsolutionstuff.com/post/laravel-56-user-roles-and-permissions-acl-using-spatie-tutorialexample.html

or 要么

https://www.google.pl/search?q=laravel+roles+and+permissions+tutorial&oq=laravel+roles+&aqs=chrome.4.69i57j69i60j69i65j0l3.11595j0j7&sourceid=chrome&ie=UTF-8 https://www.google.pl/search?q=laravel+roles+and+permissions+tutorial&oq=laravel+roles+&aqs=chrome.4.69i57j69i60j69i65j0l3.11595j0j7&sourceid=chrome&ie=UTF-8

Here is how I go about this problem in Laravel, when having users with different access-types. 当用户具有不同的访问类型时,这是我在Laravel中解决此问题的方法。

Lets say we have a model like you, called Post. 可以说我们有一个像您这样的模型,称为Post。 Now what we will do is, add a scope to that model, which we'll define a bit further down here: 现在我们要做的是,向该模型添加一个范围,我们将在这里进一步定义以下范围:

use App\Scopes\AdminScope;
class Post extends Model {

// Apply a global scope to this controller
    protected static function boot(){
        parent::boot();
        static::addGlobalScope(new AdminScope);
    }

In the router you define it as a regular resource route: 在路由器中,将其定义为常规资源路由:

Route::resource('posts', 'PostsController');

In the Controller, you can fetch all Posts on the index method like normal. 在Controller中,您可以像平常一样在index方法上获取所有帖子。 This will after we create our admin scope, return all the posts in the system for admin users, and those belonging to a specific user for regular users: 这将在我们创建管理范围后,为管理员用户返回系统中的所有帖子,对于常规用户返回属于特定用户的帖子:

class PostsController extends Controller {

public function index(){
    $posts = Post::all();
}

No comes the part where you differentiate wether returning all the posts in the system, or just the ones belonging to the current user, based on the user type that is logged in: 根据登录的用户类型,不区分返回系统中的所有帖子或仅返回属于当前用户的帖子的部分:

Create a new folder in your app-folder named Scopes. 在应用程序文件夹中创建一个名为Scopes的新文件夹。 In this folder, create a new file called AdminScope.php which will look something like this: 在此文件夹中,创建一个名为AdminScope.php的新文件,该文件如下所示:

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Auth;
class AdminScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        // IF THE CURRENT USER TYPE IS NOT ADMIN, ALTER THE QUERIES:
        if( Auth::user()->type != "admin" ){
            $builder->where('user_id', '=', Auth::user()->id)
        }
    }
}

Ofcourse, this last file you will need to alter to meet the requirements on how you differentiate between a normal user and an administrator. 当然,您将需要更改最后一个文件,以满足区分普通用户和管理员的要求。

The good thing about this approach, is that you now can apply this Scope to any model where you see it fit, and it will alter all the queries for non-admin users to only show the Models that they own where the scope is applied. 这种方法的好处是,您现在可以将此合并范围应用于您认为合适的任何模型,并且它将更改非管理员用户的所有查询,以仅显示他们拥有应用该范围的模型。

Note: 注意:

This is a global scope, that will apply to all the Eloquent Models where it has been added, and to all queries made for that model. 这是一个全局范围,将应用于添加了它的所有Eloquent模型以及对该模型进行的所有查询。 If you want, you can also write conditional local scopes, which you can read more about here: 如果需要,还可以编写条件局部范围,您可以在此处了解更多信息:

Local Scopes 本地范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM