Laravel 5.5路由访问报错“页面未找到”问题求助
Hey there, let's work through this 404 issue together—looks like there are a few straightforward fixes we can apply:
1. Fix Route Parameter Syntax (Most Likely Culprit)
Your route definitions have an incorrect syntax for parameters: you added a $ before the parameter name, which isn't allowed in Laravel route definitions.
Wrong:
Route::get('order/{$id}', 'front\FrontController@order'); Route::get('booking/{$id}', 'front\FrontController@booking');
Correct:
Route::get('order/{id}', 'front\FrontController@order'); Route::get('booking/{id}', 'front\FrontController@booking');
Laravel automatically passes the value of {id} as the $id parameter to your controller method, so you don't need the $ in the route URI.
2. Verify Controller Namespace & Directory Structure
Make sure your FrontController is properly placed and namespaced:
- The controller file should be at
app/Http/Controllers/front/FrontController.php - The top of your controller file must include the correct namespace:
namespace App\Http\Controllers\front; use App\Http\Controllers\Controller; // Don't forget to extend the base Controller use Illuminate\Support\Facades\DB; class FrontController extends Controller { // Your methods here... }
3. Clear Route & Application Cache
If you've ever run php artisan route:cache during development, Laravel might be using cached old routes. Run these commands in your terminal to clear caches:
php artisan route:clear php artisan cache:clear
4. Check Registered Routes
Run this command to list all registered routes and confirm your order/{id} and booking/{id} routes are present:
php artisan route:list
Look for entries with URIs order/{id} and booking/{id} pointing to front\FrontController@order and front\FrontController@booking respectively. If they don't show up, double-check your route file (usually routes/web.php) to ensure the routes are correctly added.
5. Validate View Link Generation
Temporarily add a debug output in your view to confirm $category->id has a valid value:
@foreach($categorys as $category) <!-- Debug line --> <p>Category ID: {{ $category->id }}</p> <div class="col-md-4 col-sm-6 col-xs-12"> <div class="hover-content"> <a href="{{ url('order/'.$category->id) }}"> <!-- Rest of your view code --> </a> </div> </div> @endforeach
If the ID is empty or null, that would generate a URL like http://your-domain/order/ which would trigger a 404. In that case, you'll need to check why $category->id isn't being retrieved correctly from the database.
Try these steps one by one—chances are the route parameter syntax fix alone will resolve your issue!
内容的提问来源于stack exchange,提问作者Shahid Hussain

