Reputation: 43
I have a question about use of Laravel's helpers (route helper in my case).
When calling a helper in controllers, it works fine. For example:
class PollController extends Controller {
public function show(Request $request)
{
$route = route('polls.show');
// returns 'http://application.app/polls/show'
$data = [
'user_token' => $request->get('token')
];
return view('polls.form')->with($data);
}
public function save(Request $request)
{
dd($request->all());
}
}
But, when calling the same helper in artisan tinker or in a command class. For example:
class Inspire extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'inspire';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display an inspiring quote';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$route = route('polls.show'); // **returns 'http://localhost/polls/show'**
$this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
}
}
It (second case) is not good for me. I tried to solve this by using constants for save the correct value and then use it on command class, but I had the same problem.
I wish solve this problem and I wish know why the behavior is different in these cases.
Thanks in advance.
Upvotes: 1
Views: 503
Reputation: 180147
The route()
helper (as well as url()
and a few bits of Laravel functionality) uses the domain name from the current HTTP request. Since Artisan commands don't have a HTTP request, Laravel falls back to the app.url
configuration setting. Change it (or, by default, your .env APP_URL
setting) from the default http://localhost
to your site's URL.
Upvotes: 1