首页 文章

从artisan命令重定向到URL

提问于
浏览
0

我正在使用Laravel 5.4 . 我有一个通常由cron运行的工匠命令,但它也可以由用户在网站内运行 . 用户在页面 /customer/123 上,然后单击带有 /customer/vat/123 链接的按钮,artisan命令完成其工作并应将浏览器重定向回 /customer/123 ,但不是出于某种原因 .
我的路线看起来像这样:

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

整个事情按预期运行只是重定向什么也没做 . 日志中没有任何错误消息,只是一个空白页面 .

在我最底层的工匠命令中,我有:

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

我希望只是将我重定向到上面的URL,但事实并非如此 .

我是否需要使用其他功能从artisan命令中重定向?

2 回答

  • 0

    首先,你的工匠命令会返回一些东西,但你没有在你的路线封闭中返回任何东西 . 显然,结果是空的 .

    其次,即使你说 return Artisan::call('...'); 它也行不通,因为call方法返回控制台命令的退出状态,而不是你在artisan命令的 handle 方法中返回的输出 .

    最后,Artisan命令永远不会返回 view . 想一想,为什么工匠命令会返回视图? Artisan命令是控制台命令,并不意味着将 responses 返回到 requests . 你有控制器

    要解决此问题,您可以执行以下操作:

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

    然后从artisan命令句柄方法中删除 return redirect()->to('/customer/123');

  • 4

    我同意Paras的分析,这是对Artisan命令的一点尴尬用法 . 我认为处理这种情况的'正确'就是将命令类中的功能抽象为一个全新的类,命令和 endpoints 都可以转向大部分繁重的工作 .

    但是,如果那个's not feasible for whatever reason, you could make your command output the URI intended for the redirect. As Paras mentioned, it'对于Artisan命令中的东西没有用,我会补充一点,那也是一种不好的做法.1129136_也是如此 . 而是使用Command方法将字符串发送到您配置的任何输出缓冲区 . $this->info($yourRedirectUrI);

    最后,要使 endpoints 闭包看到该输出,请使用 Artisan::output() 方法:

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

相关问题