Laravel routes.php 变大了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14904333/
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 07:34:24  来源:igfitidea点击:

Laravel routes.php getting big

phplaravel

提问by Chris G.

As you continually add more and more routes to Routes.php it gets bigger and bigger. How do you organize them?

随着您不断向 Routes.php 添加越来越多的路由,它会变得越来越大。你如何组织它们?

采纳答案by Heap

I usually use Group routes(because controllers tend to have the same type of filtering needed if they are related) to organize them or if you wish to/can have a smaller routes file you might want to register your controllersand do the extra validation checks of the URL's parameters inside the controller itself.

我通常使用组路由(因为控制器往往需要相同类型的过滤,如果它们相关)来组织它们,或者如果您希望/可以拥有一个较小的路由文件,您可能想要注册您的控制器并进行额外的验证检查控制器内部的 URL 参数。

回答by coffe4u

I created a directory /application/routes/ and added files in there. Each file is just a set of routes. Then in routes.php I added the following code to include them all:

我创建了一个目录 /application/routes/ 并在其中添加了文件。每个文件只是一组路由。然后在 routes.php 中,我添加了以下代码以将它们全部包含在内:

// Dynamically include all files in the routes directory
foreach (new DirectoryIterator(__DIR__.DS.'routes') as $file)
{
    if (!$file->isDot() && !$file->isDir() && $file->getFilename() != '.gitignore')
    {
        require_once __DIR__.DS.'routes'.DS.$file->getFilename();
    }
}

回答by MortalViews

even after adopting all the best practices as mentioned in the other answers, Ie: using resource controller, route groups etc.

即使采用了其他答案中提到的所有最佳实践,即:使用资源控制器、路由组等。

You can simple 'include' the route files the old fashion way. as mentioned by Chris G in this comment.

您可以以旧方式简单地“包含”路由文件。正如 Chris G 在此评论中所提到的。

you can create simple directory structure and include the route files in the route.php file.

您可以创建简单的目录结构并将路由文件包含在 route.php 文件中。

../myroutes
 --route-type-1.php
 --route-type-2.php 

In route.php file

在 route.php 文件中

include('myroutes/route-type-1.php');

There is nothing wrong in it. This is how routes are included in the packages.

这没有什么不对的。这就是路由包含在包中的方式。

http://laravel.com/docs/packages#package-routing

http://laravel.com/docs/packages#package-routing

回答by Hexodus

Actually routs should stay slim. Just move your code to controllers and use the routs to register and redirect to them. The convention is to store one controller per file so your code becomes automatically organized.

实际上溃败应该保持苗条。只需将您的代码移动到控制器并使用路由注册并重定向到它们。约定是为每个文件存储一个控制器,以便您的代码自动组织。

Take a look at this

看看这个

//  application/controllers/sample.php
class Sample_Controller extends Base_Controller
{
    public function action_index()
    {
        echo "Wellcome to the root" //www.testapp.com/sample
    }

    public function action_edit()
    {
        echo "Some editing functions here." //www.testapp.com/sample/edit
    }

     public function action_whatsoever()
    {
        echo "Put here whatever you like." //www.testapp.com/sample/whatsoever
    }

}

The controller-action route can be registered like this:

控制器动作路由可以这样注册:

//application/routs.php
Route::controller('admin::home');

Very straight forward and comfortable.

非常直接和舒适。

Update:

更新:

You can also register all your controllers with this line for the whole application automatically:

您还可以使用此行自动为整个应用程序注册所有控制器:

Route::controller(Controller::detect());

Or the controller with all actions:

或具有所有操作的控制器:

Route::controller(Controller::detect('yourcontroller'));

回答by Pratheek Rebala

Alternatively you could just store the routes in a different files and then get those files using include:

或者,您可以将路由存储在不同的文件中,然后使用 include 获取这些文件:

Route::group(stuff,function(){
  include __DIR__.DS.'routes1.php';
  include __DIR__.DS.'routes2.php';
});

That gives a nice and clean way to sort handle when there's way too much code, also you could just mention

当代码太多时,这提供了一种很好且干净的方式来对句柄进行排序,您也可以提及

Route::controller(Controller::detect());

And then structure your controllers accordingly:

然后相应地构建您的控制器:

class Account_Controller extends Base_Controller {
  public function action_login() {
      //Login
  }

  public function action_logout() {
      ...
  }

If you have to passin parameters to some function:

如果必须将参数传递给某个函数:

  public function dosomething($input){
     ...
  }

You can reach those functions like this:

您可以像这样访问这些功能:

  http://yourapp/account/login
  http://yourapp/account/logout

Then you could just call the function by appending the parameters to the URL,

然后你可以通过将参数附加到 URL 来调用该函数,

  http://yourapp.account/dosomething/somedata

If you need some methods to be protected then append them without the action_ so that they can't be accessed by the public.

如果您需要保护某些方法,则在没有 action_ 的情况下附加它们,以便公众无法访问它们。

Alternatively, you could switch to restful methods which is basically a way to respond to some input based on the type of query you get, for instance when you receive a "GET" request to the login page that means that the user needs to see the login screen, when you receive a "POST"request that would usually mean that the user is posting login data and accordingly you can respond which will help you cut down on the number of functions for more information on restful methods you can read this brilliant article by Ryan Tomayako at http://tomayko.com/writings/rest-to-my-wife

或者,您可以切换到 restful 方法,这基本上是一种根据您获得的查询类型响应某些输入的方法,例如,当您收到登录页面的“GET”请求时,这意味着用户需要查看登录屏幕,当您收到“POST”请求时,通常意味着用户正在发布登录数据,因此您可以做出响应,这将帮助您减少功能数量以获取有关 restful 方法的更多信息,您可以阅读这篇精彩的文章作者:Ryan Tomayako 在http://tomayko.com/writings/rest-to-my-wife

To use restful methods you need to mention restful to true and then append the action keyword before the function's name.

要使用 restful 方法,您需要将 restful 提及为 true,然后在函数名称之前附加 action 关键字。

public $restful = true;
public function get_login() {
 return View::make('login');
 //This function is accessible using GET request to http://yourapp/login
}

public function post_login() {
   $data = Input::get();
   //do something using the Input data
   //This function is accessible using a POST request to http://yourapp/login
}

This way you can eliminate the need for another route to process and verify the users credentials!

通过这种方式,您无需使用另一条路由来处理和验证用户凭据!

and if you don't want certain methods to be accessed using restful methods then just don't include the action keyword, (get,post,...) in the function name.

如果您不希望使用restful 方法访问某些方法,那么请不要在函数名称中包含 action 关键字 (get,post,...)。

Combining restful methods with smart routing is the most efficient way to keep your code clean and safe.

将 Restful 方法与智能路由相结合是保持代码干净和安全的最有效方法。