简体   繁体   中英

Redirect to URL from artisan command

I'm using Laravel 5.4. I have an artisan command that is normally run by cron, but it can also be run by the user from within the website. User is on a page /customer/123 , then clicks a button with a link to /customer/vat/123 , artisan command does its job and should redirect the browser back to /customer/123 , but is not for some reason.
My route looks like this:

Route::get('customer/vat/{id}', function ($id) {
    Artisan::call('app:reminder', [
        '--vat' => $id
    ]);
});

The whole thing runs as expected just the redirect does nothing. No error message nothing in logs, just a blank page.

In my artisan command at the very bottom I have:

return redirect::to('/customer/123');

which I'd expect to just redirect me to the above URL, but it's not.

Do I need to use some other function to redirect from within artisan command?

First of all, your artisan command returns something but you're not returning anything in your route closure. So obviously, the result would be empty.

Secondly, even if you were to say return Artisan::call('...'); it won't work, because the call method returns the exit status of the console command and not the output you return in the handle method of the artisan command.

Finally, an Artisan command is never expected to return a view . Think about it, why would an artisan command return a view? Artisan commands are meant to be console commands and are not meant to return responses to requests . You have controllers for that

To fix this you can do something like this:

Route::get('customer/vat/{id}', function ($id) {
    Artisan::call('app:reminder', [
        '--vat' => $id
    ]);
    return redirect()->to('/customer/123');
});

And then delete return redirect()->to('/customer/123'); from your artisan command handle method

I agree with Paras's analysis, that this is a little bit of an awkward usage of an Artisan command. I think the 'right' was to handle this situation would be to abstract-out the functionality from your command class into a brand new class that both the command and the endpoint could turn to for most of their heavy-lifting.

However, if that's not feasible for whatever reason, you could make your command output the URI intended for the redirect. As Paras mentioned, it's useless to return things from an Artisan command, and I would add, kind of bad practice to echo them as well. Instead, you use the Command methods that send string to whatever output buffer you have configured. $this->info($yourRedirectUrI);

Finally, to make your endpoint closure see that output, use the Artisan::output() method:

Route::get('customer/vat/{id}', function ($id) {
    Artisan::call('app:reminder', [
        '--vat' => $id
    ]);
    redirect(Artisan::output());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM