In kohana framework I can call controller via command line using
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?
My Answer:
Yes, it is possible to call a controller in Laravel 5 via the command line. You can achieve this by creating a custom Artisan command that will execute the controller method.
Here's an example of how you can create a custom Artisan command to call a controller method:
1. Create a new Artisan command by running the following command in your terminal:
bash
php artisan make:command CallController
2. Open the generated `CallController.php` file in the `app/Console/Commands` directory and update the `handle()` method to call the desired controller method. For example:
php
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
class CallController extends Command
{
protected $signature = 'call:controller {controller} {method}';
protected $description = 'Call a controller method via the command line';
public function handle()
{
$controller = $this->argument('controller');
$method = $this->argument('method');
$controllerInstance = App::make($controller);
$controllerInstance->$method();
}
}
3. Register the custom Artisan command in the `app/Console/Kernel.php` file by adding the following line to the `$commands` array:
php
protected $commands = [
Commands\CallController::class,
];
4. You can now call the controller method via the command line by running the following command:
bash
php artisan call:controller {controller} {method}
Replace `{controller}` with the fully qualified namespace of the controller (e.g., `App\Http\Controllers\HomeController`) and `{method}` with the method you want to call.
That's it! You have now created a custom Artisan command to call a controller method in Laravel 5 via the command line.
Rate this post
4 of 5 based on 9221 votesComments