php 通过命令行调用 Laravel 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28866821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:00  来源:igfitidea点击:

Call laravel controller via command line

phplaravel

提问by user3345632

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? If yes, how to do this?

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

回答by Bogdan

There is no way so far (not sure if there will ever be). However you can create your own Artisan Commandthat can do that. Create a command CallRouteusing this:

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

php artisan make:console CallRoute

For Laravel 5.3 or greater you need to use make:commandinstead:

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

php artisan make:command CallRoute

This will generate a command class in app/Console/Commands/CallRoute.php. The contents of that class should look like this:

这将在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],
        ];
    }

}

You then need to register the command by adding it to the $commandsarray in app/Console/Kernel.php:

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

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

You can now call any routeby 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 标头。

回答by Broshi

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] => 10etc'

最后[]app()->call()可容纳参数如[user_id] => 10等”

回答by Matt Habermehl

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

回答by ken

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

回答by Koushik Das

You can do it in this way too. First, create the command using

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

php artisan command:commandName

Now in the handleof the command, call the controller and trigger the method. Eg,

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

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

This will actually do the work. Hope, this helps.

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