Routing confusion in laravel 4 - laravel

I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.

Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.

Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.

Related

how group apiResource route and other routes together?

I am using apiResource and other routes. I grouped them like below:
Route::group(['prefix' => 'posts'], function () {
Route::group(['prefix' => '/{post}'], function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('/', PostController::class, [
'names' => [
'store' => 'create_post',
'update' => 'edit_post',
]
]);
});
all apiResource routes except index and store do not work! How should I group routes?
Your syntax for routing is wrong,
Notes
You will provide a uri for the apiResource (plural)
eg. Route::apiResource('posts', PostController::class);
Your name of resource route is wrong
Get this out https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes
it should be
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
No need of repeating Route::group, you can just write your routes like this
Route::prefix('posts')->group(function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
Your syntax is incorrect, there is a names method. See the documentation here https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes.

What does 'as' method do in Laravel

In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});

laravel 5.2 make dashboard for admin

I am new in laravel 5.2. I want to make dashboard for admin but i do not understand how to make it. I did copy all controller files in admin folder and also copied view folder in admin folder.
I did try some code which is mention below:-
Route::group(array('namespace'=>'Admin'), function()
{
Route::get('/admin', array('as' => 'admin', 'uses' => 'UserController#index'));
Route::get('/admin/register', array('as' => 'register', 'uses' => 'UserController#register'));
Route::get('/admin/login', array('as' => 'login', 'uses' => 'UserController#login'));
});
but now I want to show all controller files under admin like:-
localhost:8000/admin/users
If you want to use all route of admin on localhost:8000/admin.
You should use route group and add prefix on it, like that
Route::group(['prefix' => 'admin/'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
Route::get('login', function () {
// Matches The "/admin/login" URL
});
.......
});
Read more at this doc https://laravel.com/docs/5.2/routing#route-groups
I hope this could help you.

Prefixing route controllers

I've two route controllers within a route group:
Route::group(array('before' => 'auth'), function()
{
Route::controller('dashboard/', 'DashboardController');
Route::controller('dashboard/profile', 'DashboardProfileController');
});
That works until I add prefix key to the array:
Route::group(array('prefix' => 'dashboard', 'before' => 'auth'), function()
{
Route::controller('/', 'DashboardController');
Route::controller('/profile', 'DashboardProfileController');
});
It's weird as the first route controller works since I can access localhost/dashboard but the second fails on localhost/dashboard/profile and or localhost/dashboard/profile/edit
What's wrong here?!
It seems both of them route to one location, therefore the longest one should go first because it is interpreted as argument.
Route::group(array('prefix' => 'dashboard', 'before' => 'auth'), function()
{
Route::controller('/profile', 'DashboardProfileController');
Route::controller('/', 'DashboardController');
});

Laravel Routes Artisan

When I run artisan routes in laravel 4
auth/login/{v1}/{v2}/{v3}/{v4}/{v5}
Is this normal or is there something wrong. My routes work just wondering if there might be a bug or something. Below are my routes for auth. I'm using restful routes for auth.
Route::controller('auth','AuthController');
Route::get('AuthController/login', array('as' => 'login', 'uses' => 'AuthController#login'));
Route::get('auth/logout', array('as' => 'logout', 'uses' => 'auth#logout'));
Route::post('auth/login', array('uses' => 'auth#login'));
This is expected. When you register controllers with Route::controller() the controller inspector adds the URI wildcards. Consider the following example:
Route::controller('user', 'UserController');
You might then have a method like this on your UserController:
public function getProfile($username)
{
$user = User::where('username', $username)->first();
return View::make('profile')->with('user', $user);
}
You could then hit that method by going to localhost/yourapp/user/profile/jason
In a nut shell it allows you to pass extra parameters to a method. To me this is a very old school way of doing it, as it looks nicer like localhost/yourapp/user/jason/profile and in this case you'd need to use a route to map to the controller method.
I suggest you 2 improvements:
1 - keep a standard with URIs
You don't need Route::controller in this case. In order to mantain all routes with the same structure I would do:
Route::group( array('prefix'=>'auth,function(){ //make all auth routes starting by auth
Route::get('getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
});
It is not necessary to use group but if you app grows could be better. Without group code will be:
Route::get('auth/getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('auth/getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('auth/postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
2 - Protect your post routes
For every post and put request we've to prevent CSRF attacks like this:
Route::post('postLogin',array('before' => 'csrf','uses'=>AuthController#postLogin) );

Resources