gpt4 book ai didi

php - 从中间件更改目标 Controller /操作

转载 作者:可可西里 更新时间:2023-11-01 00:42:10 24 4
gpt4 key购买 nike

我有两个资源 /test/blabla。对于 /test 我已经注册了中间件。在中间件中,基于特定条件,我想将当前调用重定向到服务于 /blabla 资源的 Controller /操作,对用户透明(没有任何额外的客户端请求,没有 302 状态响应代码等。)。我怎样才能实现它?

最佳答案

看起来您的解决方案可能更适合路由文件。您建议在特定条件下提供不同的路线。

所以在你的 routes.php 文件中:

Route::get('test', function(){
if($condition){
return App::make('App\Http\Controllers\TestController')->index();
} else {
return App::make('App\Http\Controllers\BlaBlaController')->index();
}
});

如果您仍想在中间件中处理它,您应该能够执行与上述相同的操作:

return App::make($controller)->index(); // or whatever controller method you want to call.

如果你需要调用两组中间件,那么在构造函数中(调用你的中间件之后)检查你的条件并像上面那样调用另一个 Controller 。

关于php - 从中间件更改目标 Controller /操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32507001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com