Laravel 4:获取所有注册的路由

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

Laravel 4: get all registered routes

phplaravellaravel-4

提问by xAoc

I create helper class

我创建助手类

    namespace Services\Menu;

    class Item extends AbstactItem {

       public function add($url, $value, $attr, $params) {
            $routes = \Route::getRoutes(); //return always empty
       }

    }  

How I can get all registered routes in my class?

我如何获得我班级中所有注册的路线?

回答by bulicmatko

You can see all registered routes by running simple artisan command in your terminal:

您可以通过在终端中运行简单的 artisan 命令来查看所有已注册的路由:

$ php artisan routes

回答by Antonio Carlos Ribeiro

Assuming that this is something that works fine for me:

假设这对我来说很好用:

Route::get('/test', function()
{
    dd( Route::getRoutes() );
});

And this too

还有这个

Route::get('routes', array('uses'=>'RoutesController@routes'));

class RoutesController extends BaseController {

    public function routes()
    {
        dd( Route::getRoutes() );
    }

}

I understand that this is a problem in your end. Since you're not getting any errors you might, for example, be running this before the routes are built.

我知道这对你来说是个问题。例如,由于您没有收到任何错误,因此您可能会在构建路由之前运行它。

EDIT

编辑

Since I don't really know what's happening, I'm doing tests to see if I can get it behave the way it does in your end.

因为我真的不知道发生了什么,所以我正在做测试,看看我是否能让它像你最终那样表现。

This one also worked here:

这个也在这里工作:

Route::get('/test', function() {

    $r = new APP\MyRoutes();

    $r->routes();

});

and the class

和班级

<?php namespace APP;

class MyRoutes {

    public function routes()
    {
        dd( \Route::getRoutes() );
    }

}

I also added it to the psr-0 section of my composer.json:

我还将它添加到我的 composer.json 的 psr-0 部分:

"psr-0": {
    "APP": "app/controllers"
}

And the class file is located at ′app/controllers/APP/MyController.php′.

类文件位于'app/controllers/APP/MyController.php'。