質問

Laravel 4 ships with the php artisan routes command. This shows a list of registered routes on the command line. Instead of showing the registered routes on the command line, I would like to get its values within a controller.

The following method does exactly what I want:

Illuminate\Foundation\Console\RoutesCommand()

Unfortunately this is a protected method, so it doesn't work when I try something like this:

$rc = new Illuminate\Foundation\Console\RoutesCommand(new Illuminate\Routing\Router);

print_r($rc->getRoutes());

How can I access this method to display the registered routes in my Laravel 4 app?

Or even better; how can I access methods of any autoloaded service provider?

役に立ちましたか?

解決

You can get all routes like this:

$routes = App::make('router')->getRoutes();

foreach($routes as $name => $route)
{
    //do your stuff
}

他のヒント

I believe you would have to create a class that extends Illuminate\Foundation\Console\RoutesCommand and then you can run the method using print_r($this->getRoutes());

Here is a sample of how you can call an Artisan command from inside a Controller:

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\Output;
use Illuminate\Console\Application as ConsoleApplication;

class MyOutput extends Output {

    protected $contents = '';

    protected function doWrite($message, $newline)
    {
        $this->contents .= $message . ($newline ? "\n" : '');
    }

    public function __toString()
    {
        return $this->contents;
    }
}

class MyController extends BaseController {

    public function getRoutes()
    {
        $app = app();
        $app->loadDeferredProviders();
        $artisan = ConsoleApplication::start($app);

        $command = $artisan->find('routes');
        $input = new ArrayInput(array('command' => 'routes'));
        $output = new MyOutput();

        $command->run($input, $output);
        return '<pre>' . $output . '</pre>';
    }

}

In this case, the Artisan command is routes and we are not passing any parameter to it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top