在kohana框架中,我可以通过命令行使用调用控制器
php5 index.php --uri=controller/method/var1/var2
Run Code Online (Sandbox Code Playgroud)
是否可以通过cli调用我想要的Laravel 5中的控制器?如果是的话,怎么做?
Bog*_*dan 53
到目前为止没有办法(不确定是否会有).但是你可以创建自己的Artisan Command来做到这一点.CallRoute使用以下命令创建命令:
php artisan make:console CallRoute
Run Code Online (Sandbox Code Playgroud)
这将生成一个命令类make:command.该类的内容应如下所示:
php artisan make:command CallRoute
Run Code Online (Sandbox Code Playgroud)
然后,您需要通过将命令添加到app/Console/Commands/CallRoute.php数组中来注册该命令$commands:
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Http\Request;
class CallRoute extends Command {
protected $name = 'route:call';
protected $description = 'Call route from CLI';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$request = Request::create($this->option('uri'), 'GET');
$this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
}
protected function getOptions()
{
return [
['uri', null, InputOption::VALUE_REQUIRED, 'The path of the route to be called', null],
];
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以使用以下命令调用任何路由:
protected $commands = [
...,
'App\Console\Commands\CallRoute',
];
Run Code Online (Sandbox Code Playgroud)
请注意,此命令将返回响应,因为它将发送到浏览器,这意味着它包含输出顶部的HTTP标头.
Bro*_*shi 40
我正在使用Laravel 5.0,我使用以下代码触发控制器:
$ php artisan tinker
$ $controller = app()->make('App\Http\Controllers\MyController');
$ app()->call([$controller, 'myMethodName'], []);
Run Code Online (Sandbox Code Playgroud)
最后[]在app()->call()可容纳参数如[user_id] => 10等"
Mat*_*ehl 16
对于Laravel 5.4:php artisan make:命令CallRoute
然后在app/Console/Commands/CallRoute.php:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
class CallRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:call {uri}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'php artsian route:call /route';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$request = Request::create($this->argument('uri'), 'GET');
$this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
}
}
Run Code Online (Sandbox Code Playgroud)
然后在app/Console/Kernel.php:
protected $commands = [
'App\Console\Commands\CallRoute'
];
Run Code Online (Sandbox Code Playgroud)
打电话: php artisan route:call /path
你也可以用这种方式来做。首先,使用创建命令
php artisan command:commandName
Run Code Online (Sandbox Code Playgroud)
现在在handle命令中,调用控制器并触发方法。例如,
public function handle(){
$controller = new ControllerName(); // make sure to import the controller
$controller->controllerMethod();
}
Run Code Online (Sandbox Code Playgroud)
这实际上会完成工作。希望这可以帮助。
依赖注入不起作用
拉拉维尔 5.7
使用修补匠
// URL: http://xxx.test/calendar?filter[id]=1&anotherparam=2
$cc = app()->make('App\Http\Controllers\CalendarController');
app()->call([$cc, 'getCalendarV2'], ['filter[id]'=>1, 'anotherparam' => '2']);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42454 次 |
| 最近记录: |