Php 在Laravel 4中使用特定的ServiceProvider
Laravel4附带了Php 在Laravel 4中使用特定的ServiceProvider,php,laravel,service-provider,Php,Laravel,Service Provider,Laravel4附带了php artisan路由命令。这将在命令行上显示已注册路由的列表。我不想在命令行上显示已注册的路由,而是希望在控制器中获取其值 以下方法正是我想要的: illumb\Foundation\Console\routeCommand() 不幸的是,这是一种受保护的方法,因此当我尝试这样的方法时,不起作用: $rc = new Illuminate\Foundation\Console\RoutesCommand(new Illuminate\Routing\Router);
php artisan路由
命令。这将在命令行上显示已注册路由的列表。我不想在命令行上显示已注册的路由,而是希望在控制器中获取其值
以下方法正是我想要的:
illumb\Foundation\Console\routeCommand()
不幸的是,这是一种受保护的方法,因此当我尝试这样的方法时,不起作用:
$rc = new Illuminate\Foundation\Console\RoutesCommand(new Illuminate\Routing\Router);
print_r($rc->getRoutes());
如何访问此方法以在我的Laravel 4应用程序中显示已注册的路线
甚至更好;如何访问任何自动加载的服务提供商的方法?我相信您必须创建一个类
扩展Lightlight\Foundation\Console\RouteCommand
,然后您可以使用print\r($this->getRoutes())运行该方法代码>以下是如何从控制器内部调用Artisan命令的示例:
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>';
}
}
使用Symfony\Component\Console\Input\ArrayInput;
使用Symfony\Component\Console\Output\Output;
使用Illumb\Console\Application作为控制台应用程序;
类MyOutput扩展了输出{
受保护的$contents='';
受保护的函数doWrite($message,$newline)
{
$this->contents.=$message.($newline?\n):“”);
}
公共函数
{
返回$this->contents;
}
}
类MyController扩展了BaseController{
公共函数getRoutes()
{
$app=app();
$app->loadDeferredProviders();
$artisan=ConsoleApplication::start($app);
$command=$artisan->find('routes');
$input=newarrayInput(数组('command'=>'routes'));
$output=新的MyOutput();
$command->run($input,$output);
返回“.$output.”;
$routes = App::make('router')->getRoutes();
foreach($routes as $name => $route)
{
//do your stuff
}
}
}
在这种情况下,Artisan命令是routes
,我们不会向其传递任何参数。您可以获得如下所有路由:
$rc = new Illuminate\Foundation\Console\RoutesCommand(new Illuminate\Routing\Router);
print_r($rc->getRoutes());
更易于使用
exec('php/my/laravel/app/artisan routes',$output)
但这将返回一个字符串数组。我已经对结果进行了正则化,以便可以处理它的数据,但实际上这不是它应该使用的方式;