简体   繁体   English

从artisan命令重定向到URL

[英]Redirect to URL from artisan command

I'm using Laravel 5.4. 我正在使用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. 我有一个artisan命令,通常由cron运行,但也可以由用户从网站内部运行。 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. 用户在页面/customer/123 ,然后单击带有/customer/vat/123链接的按钮,artisan命令执行其工作,应将浏览器重定向回/customer/123 ,但出于某种原因而不是。
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? 我是否需要使用其他功能从artisan命令中重定向?

First of all, your artisan command returns something but you're not returning anything in your route closure. 首先,您的artisan命令返回了一些内容,但您没有在路线关闭中返回任何内容。 So obviously, the result would be empty. 所以很明显,结果将是空的。

Secondly, even if you were to say return Artisan::call('...'); 其次,即使您要说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. 它不会起作用,因为call方法返回控制台命令的退出状态,而不是您在artisan命令的handle方法中返回的输出。

Finally, an Artisan command is never expected to return a view . 最后,永远不会期望Artisan命令返回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 . Artisan命令旨在作为控制台命令,而不是返回responses requests responses 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'); 然后删除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. 我同意Paras的分析,这有点像Artisan命令的笨拙用法。 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. 但是,如果由于任何原因都不可行,则可以使命令输出用于重定向的URI。 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. 正如Paras所提到的,从Artisan命令return内容是没有用的,我还要补充一点,这也是一种不好的做法,也无法echo它们。 Instead, you use the Command methods that send string to whatever output buffer you have configured. 而是使用Command方法将字符串发送到已配置的任何输出缓冲区。 $this->info($yourRedirectUrI);

Finally, to make your endpoint closure see that output, use the Artisan::output() method: 最后,使用Artisan::output()方法使端点闭合看到该输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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