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.
Related
I recently started to code my own Admin panel in Laravel.
Every route was working fine, but all of a sudden the /admin route inside the Auth middleware group stopped working properly.
This are my routes inside web.php
My php artisan route:list
And the EntryController#index looks like this:
public function index()
{
//
$entries = Entry::all();
return view('admin.index', ['entries' => $entries]);
}
I'm having this problem for about 2 now, so maybe one of you know the solution.
I think you're having this issue because of how Laravel prioritises its routes.
And I think the culprit might be this route:
Route::get('/{link}', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
When you use {link} you are basically saying: "expect anything in this segment of the URI". Since the /{link} route is placed before the /admin route, and their URIs both contain only one segment, Laravel will try to resolve /{link} first.
Solution: just move the /{link} route below the /admin route. Might be best to just place it at the bottom of the list :D
I have created a laravel project. My login URL is like that:
http://localhost/project_name/public/login
I want to change the route name login to user and registration route to new-user. How can I do that?
You could write the routes yourself inside your routes/web.php file:
Route::get('user', 'Auth\LoginController#showLoginForm');
Route::get('new-user', 'Auth\RegisterController#showRegistrationForm');
These will be created next to your original login routes if you use Auth::routes(); inside your routes/web.php.
If you want to remove the Auth::routes(); you can offcourse, but you will have to add the post methods as well from the original login routes. You can check where they are going by doing php artisan route:list in your terminal to figure out how to point these and to which method.
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');
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})
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.