Call laravel controller via command line

后端 未结 5 2126
礼貌的吻别
礼貌的吻别 2020-11-29 03:17

In kohana framework I can call controller via command line using

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

Is it possible to call co

5条回答
  •  天命终不由人
    2020-11-29 03:58

    For Laravel 5.4: php artisan make:command CallRoute

    Then in 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:

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

    Call like: php artisan route:call /path

提交回复
热议问题