Laravel 8 default root route not working after route:cache - php

Getting this error when accessing default '/' route on laravel 8 after doing route:cache
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: HEAD.
Any idea why? all routes except this one is working :D
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
even api.php route is working.
can't find any solution online.
Thank you

Related

root route problem in laravel 6.2 RouteServiceProvider

i have defined these two different routes in laravel RouteServiceProvider like this:
protected function mapABCRoutes()
{
Route::prefix('abc')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/abc.php'));
}
protected function mapXYZRoutes()
{
Route::prefix('xyz')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/xyz.php'));
}
and i defined a route in abc.php
Route::get('/', function(){ return '<h1>ABC Admin</h1>'; })->name('abc.dashboard');
all defined routes in abc.php are working as well except route('abc.dashboard'). it throws a 404 with message "The requested resource /abc was not found on this server."
same thing resulting for xyz.php im working with all of this things in an ubuntu using laravel 6.2 in apache with mod rewrite enabled. i cant understand why these routes are not working? but the same type route works as well on laravels default route in web.php
Route::get('/', function () { return view('auth.login'); });
Route groups doesn't mean you can override the similar routes multiple time, it usually helps to clean the routes files. For instance, I've created separate route files for some of my main modules and put into their respective route files and map in RouteServiceProvider.
As you're using web routes here, What you can do here to prefix routes likeso,
for xyz.php
Route::group( [
'prefix' => 'xyz'],
function ( Router $api ) {
//your routes
});
and similar could be done for abc.php and so on.
Not Sure... it's may help you...
Route::group(['prefix' => 'abc'], function(){
Route::get('/', function(){ return '<h1>ABC Admin</h1>'; })->name('abc.dashboard');
});

laravel 5.5 error in routes - InvalidArgumentException Route [login] not defined

i replace my web.php whit this code, same as my code in laravel 5.2, now im using laravel 5.5, i dont have any errors in 5.2 version.
Route::get('/home', function () {
return view('home');
});
Route::get('/register', 'registerController#index');
Route::post('/register', 'registerController#register');
Route::get('/signin', 'signinController#index');
Route::post('/login', 'signinController#login');
Route::get('/logout', ['uses'=>'signinController#logout'])->middleware('auth');
Route::get('/profile', ['uses'=>'profileController#index'])->middleware('auth');
Route::get('/updateprofile', ['uses'=>'profileController#updateprofile'])->middleware('auth');
Route::post('/updateprofile', ['uses'=>'profileController#updateprofilesave'])->middleware('auth');
Route::post('/updateprofiles', ['uses'=>'profileController#updatechannelart'])->middleware('auth');
Route::get('/changepassword', ['uses'=>'profileController#indexpassword'])->middleware('auth');
Route::post('/changepassword', ['uses'=>'profileController#changepassword'])->middleware('auth');
Route::get('/article', 'articleController#index');
Route::get('/searchuser', ['uses'=>'searchController#index']); //Untuk searching user
Route::get('/searchuserpage', ['uses'=>'searchController#searchuser']); //searching user jquery
Route::get('/photos', ['uses'=>'documentationController#indexphoto'])->middleware('auth');
then i try to access url /profile which means need authenticate first, and it show me an error InvalidArgumentException Route [login] not defined. how to solve this problem. thankyou
this is my code for Authenticate.php
public function handle($request, Closure $next)
{
if(Auth::Check()){
return $next($request);
}
else{
return redirect('/signin');
}
}
The issue comes from the fact that somewhere in your code upon instantiation you're referring to a named route called 'login' but it's not defined in your web.php file.
An example of hitting this issue is you may have a redirect pointing to this route somewhere tucked away in one of your controllers, for example:
return redirect()->route('login');
To fix this issue, apply the name to the applicable route.
Route::post('/login', 'signinController#login')->name('login');
when you call a route in your project you must define route name .
such as :
<form action:"{{route('login')}}" method="post">
and in route :
Route::post('/signin', 'signinController#index')->name('login')
This is issue with the named routes. Please make sure which all places the named routes is being used.
Route::get('/signin', 'signinController#index')->name('login')
Here, you can see I named this route login and I can call this route anywhere using route('login') helper method.

How should I set up Laravel routes?

I'm a beginner in Laravel and I'm having some difficulties with routes. Actually, I'm having difficulties with a lot of things in Laravel, some of which I've managed to wrap my head around (such as migrations, seeding and authentication) but this is one of the most basic ones.
I've been creating routes based on the one that comes with Laravel. However, after much googling, something seems off. I'm not sure this is how it should be done.
My current web.php file looks like this:
Route::get('/', function () {
return view('pages.home');
});
Route::get('/about', function () {
return view('pages.about');
});
Route::get('/login', function () {
return view('login');
});
Route::get('/student', function () {
return view('profiles.student');
});
Route::get('/professor', function () {
return view('profiles.prof');
});
Route::get('/profadmin', function () {
return view('profiles.profadmin');
});
Route::get('/ident', function () {
return view('pages.ident');
});
// Authentication
Auth::routes();
Route::post('/login', function () {
return view('pages.ident');
});
Route::get('logout', 'Auth\LoginController#logout');
// Route::get('/home', 'HomeController#index')->name('home');
// Route::get('/ident', 'HomeController#ident')->name('ident');
//
// Route::get('/aluno', 'HomeController#aluno')->name('aluno');
//
// Route::get('/ident', 'HomeController#ident')->name('ident');
Also, certain pages should only be viewed by authenticated users and I'm having a hard time understanding how exactly that is done and how the routes should reflect that.
I'm sorry if this is simple stuff, but this is my first time using a PHP framework. Any help will be much appreciated.
lets suppose you want to protect the about route
then in the web.php file, replace your about route with this:
Route::get('/about', function () {
return view('pages.about');
})->middleware('auth');
now anyone hits /about and not logged in, it will be redirected to /login
if you want to know more about authentication, Laravel documentation really the best place for you:
https://laravel.com/docs/5.5/authentication#protecting-routes
First if you are beginner you should read Laravel documentation & Laracasts
In your routes you trying to show only views
Route::get('/about', function () {
return view('pages.about');
});
In Laravel 5.6 you can do it like this
Route::view('/about', 'viewName');

Laravel NotFoundHttpException on all routes

It's laravel version 5.4 and I'm using api.php routes. Following is one of my route in it:
Route::group(['namespace' => 'Api'], function() {
Route::get('auth/test',function(){
return "asd";
});
});
it's giving error on all routes even if I write route in web.php, it's not even loading home page:
http://localhost/bradforduniversityproject/public/
I have run following command too:
php artisan route:clear
nothing is working just giving NotFoundHttpException whatever i write in URL. Any help what could be the reason. thanks!
Following is my api.php file:
Route::group(['namespace' => 'Api'], function() {
Route::get('auth/test2',function(){
return "asd";
});
Route::post('auth/test','Auth\LoginController#test');
Route::post('auth/login','Auth\LoginController#login');
Route::post('auth/register','Auth\LoginController#register');
Route::post('auth/forgotPassword','Auth\LoginController#forgotPassword');
});

Laravel route parameter not working

Any help why this is not working ,I am using Laravel 5.5.23 version ,this is are my routes :
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads','ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('threads','ThreadController#index');
Route::get('threads/{channel}','ThreadController#index');
Route::get('threads/create','ThreadController#create');
Route::get('threads/{channel}/{thread}','ThreadController#show');
Route::post('threads','ThreadController#store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController#store');
Route::get('/logout' , 'Auth\LoginController#logout');
This is the ThreadController ,just the relevant methods actually :
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
}
public function index($channel = null)
{
if($channel){
//do something
}
else{
$threads=Thread::latest()->get();
}
return view('threads.index',compact('threads'));
}
The problem is when I try to access /threads/someChannel it returns not found ,so this is the problematic route : Route::get('threads/{channel}','ThreadController#index'); ,all other routes are working ,any idea why this one is not working ?
The Route::resource('threads','ThreadController') call is defining 7 routes with the prefix threads. Some of the routes you are defining yourself are being masked by this. Check php artisan route:list to see what routes the Route::resource call registers for you first. First come first serve when it comes to matching routes.
/threads/someChannel is going to match the route defined by the resource call:
GET /threads/{thread} ThreadController#show
Since you don't have the definition for show which is relevant to have, I would assume you have Implicit Model Binding happening. It is trying to bind that model to that parameter and it can't find it in the database by that ID and is causing a 404 because of it.

Categories