繁体   English   中英

通过命令行调用 Laravel 控制器

[英]Call laravel controller via command line

在 kohana 框架中,我可以使用命令行通过命令行调用控制器

php5 index.php --uri=controller/method/var1/var2

是否可以通过 cli 在 Laravel 5 中调用我想要的控制器? 如果是,如何做到这一点?

到目前为止没有办法(不确定是否会有)。 但是,您可以创建自己的Artisan Command来执行此操作。 使用以下命令创建一个CallRoute命令:

php artisan make:console CallRoute

对于 Laravel 5.3 或更高版本,您需要使用make:command代替:

php artisan make:command CallRoute

这将在app/Console/Commands/CallRoute.php生成一个命令类。 该类的内容应如下所示:

<?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],
        ];
    }

}

然后,您需要通过将命令添加到app/Console/Kernel.php$commands数组来注册命令:

protected $commands = [
    ...,
    'App\Console\Commands\CallRoute',
];

您现在可以使用以下命令调用任何路由

php artisan route:call --uri=/route/path/with/param

请注意,此命令将返回一个响应,因为它会发送到浏览器,这意味着它在输出的顶部包含 HTTP 标头。

我正在使用 Laravel 5.0 并且我正在使用以下代码触发控制器:

$ php artisan tinker
$ $controller = app()->make('App\Http\Controllers\MyController');
$ app()->call([$controller, 'myMethodName'], []);

app()->call()的最后一个[]可以包含诸如[user_id] => 10等参数'

对于 Laravel 5.4:php artisan make:command 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));
    }

}

然后在app/Console/Kernel.php

protected $commands = [
    'App\Console\Commands\CallRoute'
];

调用方式: php artisan route:call /path

你也可以这样做。 首先,使用创建命令

php artisan command:commandName

现在在命令的handle中,调用控制器并触发该方法。 例如,

public function handle(){
 $controller = new ControllerName(); // make sure to import the controller
 $controller->controllerMethod();
}

这实际上会完成工作。 希望这可以帮助。

依赖注入不起作用

Laravel 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']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM