簡體   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