I am not sure the best way to do this but basically I have some routes set up and some vendor ones as well but my kind of "catch all" route is getting called when I need the vendor "/forum" to be used.
Here are my routes:
Route::get('/', function () { return view('welcome'); });
Route::get('/contact', function () { return view('contact'); });
Route::get('/login', function () { return view('login'); });
Route::get('/signup', 'UserController#create');
Route::get('/logout', 'UserController#logout');
Route::get('/{slug}', 'PageController#show');
You can see the last route basically just gets the slug and and then in the controller I return the page by slug. The issue is with /forum the PageController#show is getting called since I assume Laravel looks at this route file before the vendor. Is there a better way of setting it up so Route::get('/{slug}', 'PageController#show'); gets called as the last possible option after vendor routes as well?
Laravel routes are loaded in order they are defined so the only way to have your /forum match before /{slug} is to make sure that route is loaded first. To guarantee it is loaded last I would suggest adding it after all the other routes are loaded in app/Providers/RouteServiceProvider.php which would look like:
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
Route::get('/{slug}', 'App\Http\Controllers\PageController#show');
}
Related
I have two routes
Route::get('events{event}', [EventController::class, 'show']);
Route::get('events/{eventType:slug}', [EventTypeController::class, 'show']);
and in the event controller
public function show(Event $event)
{
return $event;
}
Basically I expect to have both GET /events/1 and GET /events/slug working which it does with the above.
Now I would like to refactor the above routes to
Route::resource('events', EventController::class);
Route::get('events/{eventType:slug}', [EventTypeController::class, 'show']);
Now I receive Error: 404 NOT FOUND. Why isn't resources route proceeding to the next route?
If I change the above to below GET /events-types/my_slug works as expected
Route::resource('events', EventController::class);
Route::get('events-types/{eventType:slug}', [EventTypeController::class, 'show']);
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');
});
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.
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');
Let's say I am defining the following route in Laravel 5.3:
Route::resource('bands', 'BandController');
The default route example coming when you start a new Laravel project has the following:
Route::get('/', function () {
return view('welcome');
});
How do I make bands route to be the default one when no controller is called instead of welcome? Meaning /?
I did read docs here but didn't found anything. Any?
Place that block inside laravel/app/routes.php instead of a Controller (4.x)
Place that block inside laravel/app/Http/routes.php instead of a Controller (5.1)
Place that block inside laravel/app/routes/web.php instead of a Controller (5.3)
Route::get('/', function()
{
return view('welcome');
});
You can redirect default to anywhere you want, i.e.:
Route::get('/', function()
{
return Redirect::to( '/bands');
// OR: return Redirect::intended('/bands'); // if using authentication
});
As #Patrick mentioned, you can simply redirect to the /bands route. However, I have to warn you that the redirect will actually change the URL in the navigation pane of the web browser. I would have suggested that you just ask the home route to use the index method of your BandController as follows:
Route::get('/', ['uses'=>'BandController#index']);