Reputation: 21
So basically I was trying to fetch the output of a route on my application from a custom artisan command I created. I created the Request and dispatched it with the Router within the handle function and for testing, simply output the response to the console.
When I run the command from the console, I am always getting an error saying "Class web does not exist", where I believe 'web' is the middleware.
This is the first time I am trying to do this and I am stuck. I will really appreciate if you could check my code and help me figure out the cause of this error.
Here is my command:
<?php
namespace APP\Console\Commands;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Events\Dispatcher;
use Illuminate\Console\Command;
class TestCmd extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'testcmd';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
protected $request;
protected $router;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Router $router)
{
parent::__construct();
$this->router = $router;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$request = Request::create('test-route', 'GET');
$this->info($this->router->dispatch($request));
}
}
The error I am getting is:
ReflectionException in Container.php line 738: Class web does not exist
(This is just the error, the output is actually the default laravel error template that is displayed on the console)
Thanks!
Edit
To add more clarity to my question I wanted to describe my goal with this code. I want to run an artisan command from the command line that would fetch the response of a route in my application and return it. These routes are simply API end points. Please let me know if more details are required
Upvotes: 2
Views: 731
Reputation: 11
try this
public function handle()
{
$request = Request::create('test-route', 'GET');
$kernel = app()->make(\Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle($request);
$this->info($response->getOriginalContent());
}
Upvotes: 1