简体   繁体   English

通过命令行调用 Laravel 控制器

[英]Call laravel controller via command line

In kohana framework I can call controller via command line using在 kohana 框架中,我可以使用命令行通过命令行调用控制器

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

Is it possible to call controller I want in Laravel 5 via cli?是否可以通过 cli 在 Laravel 5 中调用我想要的控制器? If yes, how to do this?如果是,如何做到这一点?

There is no way so far (not sure if there will ever be).到目前为止没有办法(不确定是否会有)。 However you can create your own Artisan Command that can do that.但是,您可以创建自己的Artisan Command来执行此操作。 Create a command CallRoute using this:使用以下命令创建一个CallRoute命令:

php artisan make:console CallRoute

For Laravel 5.3 or greater you need to use make:command instead:对于 Laravel 5.3 或更高版本,您需要使用make:command代替:

php artisan make:command CallRoute

This will generate a command class in app/Console/Commands/CallRoute.php .这将在app/Console/Commands/CallRoute.php生成一个命令类。 The contents of that class should look like this:该类的内容应如下所示:

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

}

You then need to register the command by adding it to the $commands array in app/Console/Kernel.php :然后,您需要通过将命令添加到app/Console/Kernel.php$commands数组来注册命令:

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

You can now call any route by using this command:您现在可以使用以下命令调用任何路由

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

Mind you, this command will return a response as it would be sent to the browser, that means it includes the HTTP headers at the top of the output.请注意,此命令将返回一个响应,因为它会发送到浏览器,这意味着它在输出的顶部包含 HTTP 标头。

I am using Laravel 5.0 and I am triggering controllers using this code:我正在使用 Laravel 5.0 并且我正在使用以下代码触发控制器:

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

the last [] in the app()->call() can hold arguments such as [user_id] => 10 etc' app()->call()的最后一个[]可以包含诸如[user_id] => 10等参数'

For Laravel 5.4: php artisan make:command CallRoute对于 Laravel 5.4:php artisan make:command CallRoute

Then in app/Console/Commands/CallRoute.php :然后在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));
    }

}

Then in app/Console/Kernel.php :然后在app/Console/Kernel.php

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

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

You can do it in this way too.你也可以这样做。 First, create the command using首先,使用创建命令

php artisan command:commandName

Now in the handle of the command, call the controller and trigger the method.现在在命令的handle中,调用控制器并触发该方法。 Eg,例如,

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

This will actually do the work.这实际上会完成工作。 Hope, this helps.希望这可以帮助。

DEPENDENCY INJECTION WON'T WORK依赖注入不起作用

Laravel 5.7 Laravel 5.7

Using tinker使用修补程序

 // 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