【问题标题】:How to get the route its structure in Laravel如何在 Laravel 中获取其结构的路线
【发布时间】:2016-10-24 03:22:56
【问题描述】:

假设我在 Laravel 中有以下路线:

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

假设我在以下网址:posts/3/comments/54

我可以通过访问url() 方法将url 作为字符串获取,这将返回确切的url。

就我而言,我需要别的东西。有没有办法获得路线的结构?我想做这样的事情:

if(someUrlStructureFunction() == "posts/{post}/comments/{comment}") //do something

谢谢!

【问题讨论】:

  • 你想通过检查 url 结构来达到什么目的?
  • 每个用户都有特定的角色。每个请求(因此 url + 请求类型)都有单独的规则,用户可以使用哪些角色访问它。例如,给定的 url 和 POST 请求只能由管理员访问。但是,例如,任何人都可以完成 url 和 GET 请求。这些东西都保存在数据库中。我们宁愿不使用控制器的动作名称来识别它,因为这很容易在代码中被意外更改而无需在数据库中更改
  • 我认为您可以使用中间件来实现这一点,您可以在其中按每个用户角色过滤请求。了解更多:laravel.com/docs/5.3/middleware
  • @Derp 将其绑定到路由/URL 似乎很奇怪。你应该使用laravel.com/docs/5.3/authorization#via-controller-helpers之类的东西

标签: php laravel


【解决方案1】:

给你的路线a name:

Route::get('user/profile', 'UserController@showProfile')->name('profile');

(这是 Laravel 5.3 的语法。在早期版本中略有不同。)

那么,你可以使用:

Request::route()->getName()

检查您是否在该路线上。

【讨论】:

  • 确实考虑过这个选项。问题是我们必须为 200 多条路线命名,但我想没有太多其他选择。
【解决方案2】:

使用资源时,动作默认有名字,所以你的帖子可以是:

Route::resource('posts.comments', 'PostsCommentsController');

php artisan route:list
+--------+-----------+----------------------------------------+------------------------+------------------------------------------------------+------------+
| Domain | Method    | URI                                    | Name                   | Action                                               | Middleware |
+--------+-----------+----------------------------------------+------------------------+------------------------------------------------------+------------+
|        | GET|HEAD  | posts/{posts}/comments                 | posts.comments.index   | App\Http\Controllers\PostsCommentsController@index   |            |
|        | POST      | posts/{posts}/comments                 | posts.comments.store   | App\Http\Controllers\PostsCommentsController@store   |            |
|        | GET|HEAD  | posts/{posts}/comments/create          | posts.comments.create  | App\Http\Controllers\PostsCommentsController@create  |            |
|        | GET|HEAD  | posts/{posts}/comments/{comments}      | posts.comments.show    | App\Http\Controllers\PostsCommentsController@show    |            |
|        | PUT|PATCH | posts/{posts}/comments/{comments}      | posts.comments.update  | App\Http\Controllers\PostsCommentsController@update  |            |
|        | DELETE    | posts/{posts}/comments/{comments}      | posts.comments.destroy | App\Http\Controllers\PostsCommentsController@destroy |            |
|        | GET|HEAD  | posts/{posts}/comments/{comments}/edit | posts.comments.edit    | App\Http\Controllers\PostsCommentsController@edit    |            |
+--------+-----------+----------------------------------------+------------------------+------------------------------------------------------+------------+

然后你可以使用Request::route()->getName()来分析调用了哪一个。

【讨论】:

  • 我想这也能胜任! Laravel 没有获取路由签名的东西仍然很有趣,我想有时可能有一个实际需要的原因。
【解决方案3】:

根据您的评论,您希望将某些路由限制为特定角色

您可以尝试使用路由组,然后为每个角色创建一个中间件:https://laravel.com/docs/5.3/routing#route-groups

//put your admin routes inside this
Route::group(['middleware' => 'admins'], function () {
    Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
        //
    });
});

//put your users routes inside this
Route::group(['middleware' => 'users'], function () {
    Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
            //
    });
});

【讨论】:

  • 这不适用于我们经常使用的资源路由。有时允许看到 Show 方法而不允许看到 create 方法
  • 那么控制器中的每个方法也有不同的作用?
  • 是的。例如,每个人都可以看到帖子,但不是每个人都可以创建帖子。这是用系统中的角色定义的。
  • 不,只是数据库中的一个表,其中包含角色和用户以及与其相关的角色。但我认为@ceejayoz 他的评论实际上非常简洁,正是我想要的。
猜你喜欢
  • 2016-08-03
  • 2016-05-09
  • 2021-10-24
  • 2020-04-18
  • 2021-07-22
  • 2020-12-12
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多