PHP Version – 8.2.4
Laravel 10
I am facing an issue with one of the routes in my Laravel application. I have defined a route for the ‘/about-us’ URL, but whenever I try to access it, I get a 404 NOT FOUND error. I’ve spent some time debugging it, but I can’t seem to figure out what’s causing the problem.
Here’s the code for the route in my web.php file:
Route::get('/about-us', [SiteController::class, 'about'])->name('about-us');
And here’s the corresponding method in the SiteController:
<?php
namespace AppHttpControllers;
use AppModelsTextWidget;
use IlluminateHttpRequest;
use IlluminateViewView;
class SiteController extends Controller
{
public function about(): View
{
$widget = TextWidget::query()
->where('key', '=', 'about-page')
->where('active', '=', 1)
->first();
return view('about', compact('widget'));
}
}
All other routes are functioning as expected.
I have also checked the database, and there is a record with the key ‘about-page’ and ‘active’ set to 1 in the ‘TextWidget’ table.
Thank you in advance for your time and assistance!
Tags: laravel, laravel-routing
I also Cleared the Route Cache and checked.
Restarted the Development Server but not working.
2
Answers
Probably you have to check if the view name Is ‘about’ and if the view ‘about’ Is in another folder in the views folder. Remember that if you use the same call method on the same Route you could have problems too. Maybe you could try to avoid to write ‘public function about(): View’ and write instead ‘public function about(){}’.
i have a question. Do you have maybe another Route before that Route with the same url code and method? such as Route::get(‘/about-us/{id}’, [SiteController::class, ‘about’])->name(‘about-us’). In this case you should put that Route as the last Route with this url code.