我想要重定向(301)一些包含以下内容的路线 -page=1
(查询字符串)或index.php
或者?&
(请求参数)
我已将路线添加为 -
Route::get('/{any_url}', 'UsersController@processRedirect')->where('any_url', '(.*)index\.php(.*)|(.*)page=1(.*)|(.*)|?&(.*)');
当我办理登机手续时 https://regex101.com/它正在工作,但在我的应用程序中无法正常工作。
可能是什么问题?
我已通过 MiddleWare 完成此操作,但我不想检查所有网址。
有没有其他方法可以实现这一目标?
最佳答案
因此,诀窍不是在路由中执行此操作,而是创建一个全局捕获所有路由来捕获所有路由,然后您可以在 Controller 中对路径或查询字符串进行任何处理。这使您的路由保持干净且易于使用,并允许更多地处理查询字符串和路径或基本 url。请务必使用此捕获所有路由删除要处理的路由。
路线/web.php
//make this last route to catch
Route::any('/{any}', "ProcessRequestController@handler")->where("any", ".*");
App/Http/Controllers/ProcessController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProcessRequestController extends Controller{
public function handler(Request $request){
//from here you can now access each section of the route a little more conviently.
$request->path();
if($request->has('page')){
}
}
}
关于php - Laravel 路由检查多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61338046/