How to handle root route with parameter in laravel? - php

So I've got the current route setup:
Route::get('/{id}', 'MainController#index');
This is for passing in a id through / but I would also like:
Route::get('/admin', 'AdminController#index');
but it keeps handling admin as a URL parameter rather than it's own route, is there a way of distinguishing between the two?

You need to move this route to the end of the routes file to make all other routes similar to '/admin' work:
Route::get('/{id}', 'MainController#index');

Related

How can I change the default login URL in Laravel 7.30.0

I have implemented "artisan ui vue --auth" module in my Laravel project and it's all working fine. I want to know if I can change the default login URL to some other URL e.g. 'controlcenter/login'.
I tried to change the route() in redirectTo method in Authenticate() middleware and cleared all type of cache, but no luck.
I am using Laravel 7.30.0
My routes in web.php
Route::get('/login', 'Auth\LoginController#login');
Route::get('/logout', 'Auth\LoginController#logout');
Auth::routes();
First remove all the three route declarations you have done, because Auth::route() will add all the necessary routes for you. Instead add the following:
Auth::routes(['login' => false]);
Route::prefix('controlcenter')->group(function () {
Route::get('/login', 'Auth\LoginController#showLoginForm')->name('login');
});
The Auth::routes() function will add all the routes except for the login route. The next block of code wraps around any route and prefixes them with some text, here controlcenter.
Then the login route is defined inside the prefix block to get the prefix. You can add any other routes in this block as well and all of them will get the prefix.

Prevent access to /index (default Controller action)

My routes.php looks like this:
Route::controller('/', 'HomeController');
The default root action is getIndex(). Typing the address [DOMAIN NAME]/index though, returns the start page as well. How do I prevent this? I want the start page to only be accessible when going to the root URI of my project (/).
You are using RESTful Resource Controllers, replace it with,
Route::get('/', 'HomeController#getIndex');
OTHER OPTIONS
/ refers to index method in the controller by default, you could make index refer to another method which creates the view you want like.
Route::get('index', 'SomeController#someMethod');
in your route.php file
If you want the page to show a 404 you can use in your SomeController#someMethod
public function someMethod()
{
App::abort(404)
}
Or without the use of a method
Route::get('index', function() {
App::abort(404)
})
Like pointed out in the comments, you need to blacklist the /index route. But only that one! You don't need to blacklist every route you don't want your users to see, because Laravel will handle that for every route that is not defined. But index is a special case, because you have to name the default route some name, don't you?
So long talk, short meaning: Rest assured, you will only have to blacklist each index route, if it is that important to you.
I solved it like this:
Route::get('/', 'HomeController#index');
Route::controller('/', 'HomeController');
There's a reason I want a controller that takes care of all my routes :)

Laravel controller define with mask

I have PagesController defined in my routes file:
Route::controller('/', 'PagesController');
But i use some more routes like:
Route::get('/admin', function()
{
....some code here
});
My second route doesn't work, because all other routes try to find functions in PagesController. I can change my controller to:
Route::controller('pages', 'PagesController');
But then in my home page, all links will be like www.test.com/pages/..., but i don't need that 'pages' in there. How to define my controller with mask or something like that?
Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method:
Route::controller('pages', 'PagesController')
This is a single route to define all actions in a controller using REST naming conventions therefore you get the /pages.
For the root of your app you need to specify the method that you want to call within your PagesController.
Example:
Route::get('/', array('as' => 'home', 'uses' => 'PagesController#getIndex'));
Place this line at the top of your routes in the routes file.
Change the order of your route definition to this:
Route::get('/admin', function()
{
....some code here
});
Route::controller('/', 'PagesController');
It will now look for /admin first, and if it cant find it, it when they go to your other routes...
Try changing Route::controller({same content as the question}) to Route::resource({same content as the question})

Routing in laravel framework

I using the laravel framework of php for development.I done these following
steps
I define Route::resource('users', 'UsersController'); in route file and then define Route::get('user/pingme', 'UserController#pingme');
When i make a get call to pingme function, it was not working .I was getting the response code is 200 but code inside that pingme function was not working and i do not know why.
then i changed it to Route::post('user/pingme', 'UserController#pingme'); it was working fine as needed.
then what i did is, removed Route::resource('users', 'UsersController'); and make again get route to ping me function and make get call and it starts working fine .
so this is any bug in framework(rare thing) or i am missing something(probably yes)? Help me out....
Route file works as follows:-
if you have wrote a mapping for controller only, then it needs to come at the bottom of all other route mapping otherwise your program controller will pick route from user controller only and will redirect to UserController.
so the right order of all routes is:-
Route::get('user/pingme', 'UserController#pingme');
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::resource('user', 'UserController');
OR
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::get('user/pingme', 'UserController#pingme');
Route::resource('user', 'UserController');
In your route file, the order of the routes needs to be as follows:
Route::get('user/pingme', 'UserController#pingme');
Route::post('user/logout', 'UserController#logout')->before('auth');
Route::resource('user', 'UserController');
If Route::resource('user', 'UserController') comes before the other routes, the GET request to user/pingme will be handled by show method inside of UserController, because it is how Resourceful Controllers work. So, the Route::resource for user needs to come after all other routes with user/ prefix.

Laravel4 route pattern error

I´m using laravel 4 for a cms project, and i´m having some problems with my routes...
These are my current routes
Route::get('/', 'IndexController#showNews');
Route::get('/logout', 'UserController#logout');
Route::resource('/login', 'UserController');
Route::resource('/user', 'UserController#index');
Route::resource('/user/{route}', 'UserController');
// Routes that shows us the pages...
Route::get('/{page}', 'IndexController#showPage');
Route::get('/{page}/{id}', 'IndexController#showPage');
To my user routes i have a custom router that routes the user information around, not really a problem. But all of that works great, but when i try to navigate to "/test" Wich would link to a test page, it gives me this error.
Route pattern "/user/{route}/{{route}}" cannot reference variable name "route" more than once.
It comes up to router logic, and i´m fairly new to laravel. Is there a way for me to work around this problem? It´s a collision between the user/route and the /route wildcards.
Route::resource('/user', 'UserController#index');
Route::resource('/user/{route}', 'UserController');
The problem is that you are using Route::resource to declare the routes, while by using Route::resource you are actually declaring a RESTful controller with a table of actions to be handled by Laravel automatically. You are using it incorrectly.
See the docs to see which routes are handled in the background (and hence the source of the conflict):
Take a look at the table called Actions Handled By Resource Controller
For any route handler that is not within the table you will have to declare separate routes. Something like:
Route::get('foo/filter/{filterName}/{filterValue}',
array('as'=>'filteredroute','uses'=>'FooController#filter'))
As a summary, Route::resource enables you quick CRUD RESTful access.

Categories