简体   繁体   English

laravel路由,如何在一个文件中将相似分组

[英]laravel routes, how to group similar in one file

I'm working with laravel (5.2), and there are a lot routes in my route file. 我正在使用laravel (5.2),并且我的路线文件中有很多路线。

in fresh install I noticed that it was loading auth routes something like this. 在全新安装中,我注意到它正在加载类似这样的auth路由。

Route::auth();

nothing else was there in routes.php file related to auth routes. auth路由相关的routes.php文件中没有其他内容。

in my file, I've like this one 在我的档案中,我喜欢这个

Route::get('color/event', 'ColorController@index');
Route::post('color/event', 'ColorController@post_message);
...
...

and many others, So I want to load all in laravel way, like Route::color(); 还有很多其他东西,所以我想以laravel方式加载所有laravel ,例如Route::color(); laravel and it should load all color related routes 并且应该加载所有与颜色相关的routes

Thanks for you time 谢谢你的时间

you can try this 你可以试试这个

Route::resource('admin/settings','Admin\SettingsController');

and try this command 并尝试此命令

$ php artisan routes

Using Route::get() , Route::post() and similar functions is doing it the Laravel way - see the docs here https://laravel.com/docs/5.2/routing#basic-routing 使用Route :: get()Route :: post()和类似的函数以Laravel的方式进行操作 -在此处查看文档https://laravel.com/docs/5.2/routing#basic-routing

Route::auth() is just a helper function introduced in Laravel 5.2 to keep all auth definitions together. Route :: auth()只是Laravel 5.2中引入的一个辅助函数,用于将所有auth定义保持在一起。

so, anyone if s/he is looking for same answer, I figured that out. 因此,如果他/她正在寻找相同的答案,我就知道了。

if you want something like Route::auth(); 如果您想要类似Route::auth(); OR Route::color();//in my case or whatever you want to call it, you need to add custom function in your Router.php file. Route::color();//in my case或任何您想调用的名称,您需要在Router.php文件中添加自定义function So solution will look like 所以解决方案看起来像

//inside Router.php file
public function whatever(){
    $this->get('app/', 'AppController@index');
    $this->post('app/new', 'AppController@create');
}

and in your route.php file, you can do this. 在您的route.php文件中,您可以执行此操作。

Route::whatever();

But this is really dirty way to do that 但这确实是一种肮脏的方式

so instead you can extend the base Router and register your router in bootstrap/app.php 因此,您可以扩展基本Router并在bootstrap/app.php注册您的路由器

$app->singleton('router', 'App\Your\Router');

so I community forces to use second approach. 所以我社区部队使用第二种方法。

for more details, have a look here. 有关更多详细信息,请在这里查看。

hope someone will find this useful 希望有人会觉得有用

Thanks. 谢谢。

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

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