Use of "uses" in router of Laravel - laravel-5

I have written this and it is working.
Route::get( 'users', [ 'uses' => 'UsersController#index'] );
Now i am removing the "uses" and it is still working.
Route::get( 'users', 'UsersController#index' );
What is the requirement of "uses" here. Pleas give the view with reference url.

Related

Why does Laravel api's are returning 405 error?

So my scenario is:
Route::group(['middleware' => 'api'], function ($router) {
Route::post('/login', [
'as' => 'login',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#login',
]);
Route::post('/register', [
'as' => 'register',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#register',
]);
Route::get('/logout', [
'as' => 'logout',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#logout',
]);
});
Here Login API is working perfectly using postman but register and logout api's are returning 405 method not allowed error.
Note: I have already checked that I must be using post and get correctly. These API's were also working right previously. I don't know what has made them stop.
In case if someone wants to have a look at controller functions, here is the gist. Looking forward to the suggestions.

I have problem with slug and id in web route

i have this code
Route::get('posts/{id}/postedit','PostController#postedit');
Route::post('posts/{id}/postupdate','PostController#postupdate');
Route::get('posts/{id}/postdelete','PostController#postdelete');
Route::get('/{slug}', [
'uses' => 'SiteController#singlepost',
'as' => 'site.single.post'
]);
Route::get('/{id}', [
'uses' => 'PendaftaranAlumniController#test',
'as' => 'test',
]);
so here is the problem, when i run like this it Route::get('/{slug}' it works fine but Route::get('/{id}' it doesnt work
otherwise when i put Route::get('/{id} in the top it works fine, but Route::get('/{slug}' doesn`t work anymore.
how can i solve this?
Because both /{slug} and /{id} is getting treated as same by Laravel, both the routes /{slug} and /{id} will point any route defined earlier.
You can use it as it is, you need to tell Laravel that these two routes are different in any manner so that Laravel will treat it differently.
Route::get('singlepost/{slug}', [
'uses' => 'SiteController#singlepost',
'as' => 'site.single.post'
]);
Route::get('test/{id}', [
'uses' => 'PendaftaranAlumniController#test',
'as' => 'test',
]);

Two routes in laravel getting confused, I need a solution to this

1. Route::get('/admin', [
'uses' => 'AdminController#admin',
'as' => 'admin' ]);
2. Route::get('/{uri}', [
'uses' => 'ArticleController#article',
'as' => 'article' ]);
if I try to go to admin route it goes to article route, I need a solution for both routes to work.
The conflict arrives in your case due to /{uri} matches to /admin ,
For example. /xyz, /abc and even /admin match to /{uri} route.
You can do something like this to avoid it.
Route::get('/admin', [ 'uses' => 'AdminController#admin', 'as' => 'admin' ]);
Route::get('/uri/{uri}', [ 'uses' => 'ArticleController#article', 'as' => 'article' ]);
Note that now /admin will match to your /admin route and /uri/{parameter} will match to your /uri/{uri} route
You have to write route 1 first then route 2
Follow: Laracasts

Behaviour of routing in laravel

After trying out this acl tutorial I came across something I can't understand.
In laravel I created my route according to the tutorial and changed it to match laravel's auth controller (as I already used that before by installing it with bestmomo) to:
$router->get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Like this it doesn't find my route giving me the error
NotFoundHttpException in RouteCollection.php line 161:
If I add the following route:
Route::get('/login', [
'as' => 'login', 'uses' => 'Auth\AuthController#getRegister'
]);
It works well.
Why do I need to add the second route?
Why can't the first one stand alone?
I think the blog you are following has an error.
The correct way to call a route you need is:
Route::get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Note: you can also use a helper method and just call:
get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);;
The only way I can see the the code you mentioned working is if the following is at the top of the routes file:
$router = app('router');

two different controllers with same route name

Assume I have two controllers named Client\DashboardController and Admin\DashboardController and routes.php contains the following:
Route::get('admin/', [
'uses' => 'Admin\DashboardController',
'as' => 'dashboard'
]);
Route::get('client/', [
'uses' => 'Client\DashboardController',
'as' => 'dashboard'
]);
both controllers are in different namespace but share one view. In my view I would like to do the following:
home
and depending on the url prefix it should maps to the correct controller, but the issue is it always maps to Client controller.
That's like wanting to have two function with the same name just for the sake of it and complain about the compiler for not doing what you want to.
Just namespace your routes, it'll make you life (debugging and maintaining) easier:
Route::get('admin/', [
'uses' => 'Admin\DashboardController',
'as' => 'admin.dashboard'
]);
Route::get('client/', [
'uses' => 'Client\DashboardController',
'as' => 'client.dashboard'
]);
And then:
home
home

Resources