Laravel 8迁移至DigitalOcean后分类页面跳转异常求助
Laravel 8迁移后分类页面跳转异常问题排查与解决
问题概述
将Laravel 8项目从name.com主机迁移至DigitalOcean主机后,前后端分类页面URL均跳转至首页或已有页面,但本地环境运行正常。服务器端var_dump请求发现query_string为空,不过dd()输出的Illuminate\Database\Eloquent\Collection原始数据包含正确id。
相关代码片段
DD输出结果
#Illuminate\Database\Eloquent\Collection {#3783 ▼ #items: array:19 [▼ 9 => App\SubCategory {#3793 ▼ #original: array:8 [▼ "id" => 17 ]
路由定义
Route::get('browse/subcategory/', 'CategoriesController@subcategorypage')->name('subcategory.page');
控制器代码
public function subcategoryPage(Request $request){ $ipaddress = $request->getClientIp(); $geoip = geoip()->getLocation($ipaddress); $usercountry = strtoupper($geoip->country); $cats = SubCategory::where('id', $request->id)->first(); // dd($cats); if (!$cats) { return redirect('/')->with('delete', '404 | category not found !'); } if ($request->type) { $courses = $cats->courses()->where('status', '1')->where('type', '=', $request->type == 'paid' ? '1' : '0')->paginate($request->limit ?? 10); } else if ($request->sortby) { if ($request->sortby == 'l-h') { $courses = $cats->courses()->where('status', '1')->where('type', '=', '1')->orderBy('price', 'DESC')->paginate($request->limit ?? 10); } if ($request->sortby == 'h-l') { $courses = $cats->courses()->where('status', '1')->where('type', '=', '1')->orderBy('price', 'ASC')->paginate($request->limit ?? 10); } if ($request->sortby == 'a-z') { if ($request->type) { $courses = $cats->courses()->where('status', '1')->where('type', '=', $request->type == 'paid' ? 1 : 0)->orderBy('title', 'ASC')->paginate($request->limit ?? 10); } else { $courses = $cats->courses()->where('status', '1')->orderBy('title', 'ASC')->paginate($request->limit ?? 10); } } if ($request->sortby == 'z-a') { if ($request->type) { $courses = $cats->courses()->where('status', '1')->where('type', '=', $request->type == 'paid' ? 1 : 0)->orderBy('title', 'DESC')->paginate($request->limit ?? 10); } else { $courses = $cats->courses()->where('status', '1')->orderBy('title', 'DESC')->paginate($request->limit ?? 10); } } if ($request->sortby == 'newest') { if ($request->type) { $courses = $cats->courses()->where('status', '1')->where('type', '=', $request->type == 'paid' ? 1 : 0)->orderBy('created_at', 'DESC')->paginate($request->limit ?? 10); } else { $courses = $cats->courses()->where('status', '1')->orderBy('created_at', 'DESC')->paginate($request->limit ?? 10); } } if ($request->sortby == 'featured') { if ($request->type) { $courses = $cats->courses()->where('status', '1')->where('type', '=', $request->type == 'paid' ? 1 : 0)->where('featured', '=', '1')->paginate($request->limit ?? 10); } else { $courses = $cats->courses()->where('status', '1')->where('featured', '=', '1')->paginate($request->limit ?? 10); } } } else if ($request->limit) { // return 'ghjj'; if ($request->limit == '10') { $courses = $cats->courses()->where('status', '1')->paginate(2); } elseif ($request->limit == '30') { $courses = $cats->courses()->where('status', '1')->paginate($request->limit); } elseif ($request->limit == '50') { $courses = $cats->courses()->where('status', '1')->paginate($request->limit); } elseif ($request->limit == '100') { $courses = $cats->courses()->where('status', '1')->paginate($request->limit); } else { $courses = $cats->courses()->where('status', '1')->paginate($request->limit ?? 10); } } else if ($request->lang) { $lang = CourseLanguage::where('id', $request->lang)->first(); $courses = $cats->courses()->where('status', '1')->where('language_id', '=', $lang->id)->paginate($request->limit ?? 10); } else { $courses = $cats->courses()->where('status', '1')->paginate($request->limit ?? 10); } $filter_count = $courses->count(); $childcat = ChildCategory::where('subcategory_id', $cats->id)->get(); return view('front.category', compact('cats', 'courses', 'childcat', 'filter_count', 'usercountry')); }
问题排查与解决
1. 服务器Web服务配置修复
服务器query_string为空,核心原因是Nginx/Apache未正确将Query String传递给Laravel应用:
- Nginx配置调整:在站点配置的
location /块中添加try_files $uri $uri/ /index.php?$query_string;,完整示例:
location / { try_files $uri $uri/ /index.php?$query_string; }
- Apache配置调整:确保启用
mod_rewrite模块,且项目根目录的.htaccess文件包含正确的重写规则(参考Laravel官方默认.htaccess内容)。
2. 路由参数传递优化
避免依赖Query String,改为路由参数传递的方式:
// 修改路由 Route::get('browse/subcategory/{id}', 'CategoriesController@subcategorypage')->name('subcategory.page'); // 控制器修改参数接收逻辑 public function subcategoryPage(Request $request, $id){ $cats = SubCategory::find($id); // 其余代码保持不变 }
这种方式更符合RESTful规范,也能规避Query String解析失败的问题。
3. 清理迁移后缓存
执行以下命令清理Laravel各类缓存:
php artisan cache:clear php artisan route:clear php artisan config:clear php artisan view:clear
4. 验证环境变量配置
确保.env文件中的APP_URL与当前站点域名一致:
APP_URL=https://your-domain.com
内容的提问来源于stack exchange,提问作者imon
相关产品推荐
相关产品推荐

