I have a Laravel 8 project. Everything work fine except the code below
Route::get('/', function ()
{
return redirect('/login');
});
Those code only run when i clear the route cache with php artisan route:clear or it will return this error everytime after i cache route:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: HEAD.
I want my users can go to login page despite of they access the http://localhost/myproject/public/ or http://localhost/myproject/public/login
If anyone have the answer, pleas help me. Thank you!
enter image description here
1.Add this at the top of your web.php use Illuminate\Support\Facades\Route;
2.
Route::get('/greeting', function () {
return 'Hello World';
});
Routing Laravel
I am creating a simple website in Laravel while running a web site ran into the problem with 404 NOT FOUND Laravel 8. index page working when I click on about us and contact us page getting error of 404 NOT FOUND I don't know why is that. what I tried so far I attached below.
Folder structure
Controller
class SmsController extends Controller
{
public function index()
{
return view('index');
}
public function aboutus()
{
return view('aboutus');
}
public function contactus()
{
return view('contactus');
}
}
Index.blade.php I made the links
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Aboutus</li>
<li>Contactus</li>
</ul>
routes
Route::get('/', 'App\Http\Controllers\SmsController#Index');
Route::get('Aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('Contactus', 'App\Http\Controllers\SmsController#contactus');
I think you got error here
Route::get('Aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('Contactus', 'App\Http\Controllers\SmsController#contactus');
instead
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('/contactus', 'App\Http\Controllers\SmsController#contactus');
I use this command:
php artisan route:cache
then
php artisan serve
After that, copy the IP address after second command.
In laravel 8 you can try this way:
Route::get('/', [App\Http\Controllers\SmsController::class, 'index']);
Route::get('/about-us', [App\Http\Controllers\SmsController::class, 'aboutus']);
Route::get('/contact-us', [App\Http\Controllers\SmsController::class, 'contactus']);
I had to use php artisan route:cach command in terminal.
In my laravel 9 case, I added a new attribute style routes and it gave 404 error, from console writing
`php artisan route:clear`
has solved my issue.
try to write a route like this
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');
Thank you guys, I have been on this for close to two days, this is what helped me
php artisan route:cache
using v9
The problem is with the route address since the Laravel route are case sensitive.
the correct form of route
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('/contactus', 'App\Http\Controllers\SmsController#contactus')'
and also it is worth mentioning that the updated accepted way to write routes in the Laravel 8.x to-class method is
Route::get('/aboutus', [App\Http\Controllers\SmsController::class, 'aboutus']);
Route::get('/contactus', [App\Http\Controllers\SmsController::class, 'contactus']);
but the old method is still accessible on the newer version of Laravel
i have install fresh laravel 5.7. now when i declare route in web.php. when i declare like
Route::get('foo', function () {
return 'Hello World';
});
it redirect to 404 page but.. when i declare my root directory name along with route just like
Route::get('blog/foo', function () {
return 'Hello World';
});
it shows me expected result..
but i don't want to define like that. i want to define like this and get the expected result
Route::get('foo', function () {
return 'Hello World';
});
help me !
It should work perfectly fine but try the following command and let me know if it fixes your issue
php artisan route:clear
There should be a route like /{someParam}
move your route '/foo' up so it is placed above param /{someparam} route
I have following Controller Action Method.
namespace App\Http\Controllers\API\SportsType;
class SportsTypeApiController extends \App\Http\Controllers\Controller
{
public function apiSportsTypes() {
return 1;
}
}
Here is the route
Route::group(['prefix' => 'api/v1'], function () {
Route::get('/apiSportsTypes', 'API\SportsType\SportsTypeApiController#apiSportsTypes');
});
It gives 404 error. Am I missing something? Please let me know if you need more details.
It was happening because the routes were using cache. Due to that new routes were not even appearing in command prompt even if I was typing below command.
php artisan route:list
Then I had to remove the route cache by using the below command
php artisan route:clear
and now everything is working fine.
Hey so I'm getting this weird error
MethodNotAllowedHttpException
I cannot find a solution anyway on the internet, any advice please?
Here it is:
Route::post('api/avatarDetails/{avId}', 'API\APIController#doAvatarDetails');
Route::get('api/avatarDetails', function() {
return Redirect::to('/')->with('failure', 'You lack the ability to do that young one.');
});
There you go #Alexy
Route::get('/', function() {
return view('welcome');
});
I don't see this route, but you still trying to redirect to it:
Route::get('/', function () {
//
});
If you have one, run php artisan route:clear command which will clear all old route cache.