重定向在Laravel AppServiceProvider.php中定义的函数中不起作用

j9per5c4  于 2023-01-27  发布在  PHP
关注(0)|答案(1)|浏览(175)

重定向在Laravel AppServiceProvider.php中定义的函数中不起作用

    • 应用程序服务提供程序. php**
public function register()
{
    require_once __DIR__ . '/../Functions/functions.php';
}
    • 函数. php**
function test(){

        if($x == $y){
        return redirect()->route('products')->with(['type' => 'error', 'message' => 'Oops.']);
        }else{
         //!I don't want the code to do anything and I want it to go back to the controller and continue running.
        }
vlurs2pr

vlurs2pr1#

test()函数与return配合使用,以便将其重定向到您定义的路由。
当您调用test()进行重定向时,请按以下方式记录

return test();

已更新函数中的代码

根据条件返回route or false检查条件

function test(){
    if($x == $y){
        return redirect()->route('products')->with(['type' => 'error', 'message' => 'Oops.']);
    }else{
        //!I don't want the code to do anything and I want it to go back to the controller and continue running.
        return false;
    }
}

调用函数时更新代码

if(test()){
   return test();
}

相关问题