Laravel Is it possible to pass a variable in route group? - php

I have following routes:
Route::group(['prefix' => 'group1'], function () {
Route::get('view1', ['as' => 'group1_view1', 'uses' => 'group1Controller#get_view1']);
Route::get('view2', ['as' => 'group1_view2', 'uses' => 'group1Controller#get_view2']);
});
Route::group(['prefix' => 'group2'], function () {
Route::get('view1', ['as' => 'group2_view1', 'uses' => 'group2Controller#get_view1']);
Route::get('view2', ['as' => 'group2_view2', 'uses' => 'group2Controller#get_view2']);
});
I wish to pass variables, for example, $title = 'group-one' to all views in group1 and $title = 'group-two' to all views in group2. Instead of adding variable $title in all methods in each group controller, is it possible to pass variables groupwise in routes?

NO you cannot pass variables into route groups, but if in case you need one piece of view/code in multiple views, title for example, you can use view composers instead.

Related

Laravel named routes grouping all admin routes

i just want to group all my admin routes in my laravel. I'm a beginner in laravel and i want to synchronize all my admin routes in one group, my question is, why i cant put the post route inside the group of my admin routes?
Here is my routes:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
});
my above code was returning error , where laravel says admin.login route doesn't exist. Then i tried to put the post route outside the group and it works. Why?.
Here is the code where returns no error:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
});
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
Because you use as in your route group and it's admin:: and you may link to admin.
Now it goes to admin::login and you need admin.login

Laravel doesn't return edit view

im using laravel 5.5 and i created a view for edit categories inside views/back/categories/edit
here my edit function in CategoriesController
public function edit($id)
{
$category = Categories::getall();
$categories = Categories::find($id);
return view('back.categories.edit', ['categories' => $categories, 'category' => $category]);
}
and here is my route
Route::group(['middleware'=>'admin'],function(){
Route::get('/dashboard','BackendController#index')->name('backend');
Route::group(['prefix' => 'categories'], function () {
Route::any('/show/{id}', ['as' => 'backend.categories.show', 'uses' => 'backend\CategoriesController#show']);
Route::get('/index', ['as' => 'back.categories.index', 'uses' => 'backend\CategoriesController#index']);
Route::any('/store', ['as' => 'back.categories.store', 'uses' => 'backend\CategoriesController#store']);
Route::any('/create', ['as' => 'back.categories.create', 'uses' => 'backend\CategoriesController#create']);
Route::any('/edit/{id}', ['as' => 'back.categories.edit', 'uses' => 'backend\CategoriesController#edit']);
Route::any('/update', ['as' => 'back.categories.update', 'uses' => 'backend\CategoriesController#update']);
Route::any('/destroy/{id}', ['as' => 'back.categories.destroy', 'uses' => 'backend\CategoriesController#destroy']);
});
});
my edit button
<a href="{{ url('back/categories/edit/'.$category->cat_id) }}" class="btn btn-success btn-sm">
<span class="fa fa-edit"></span> edit</a>
when i click on the edit button it return page not found with "Sorry, the page you are looking for could not be found." text and the URL :"back/categories/edit/4"
Since your route are named, you can use the route() helper to build working URL:
{{ route('back.categories.edit', ['id' => $category->cat_id]) }}
From your routes definition it looks like your url is /categories/edit/{id}
For links in templates I suggest to use the route helper function which takes the route name or the action method which takes controller#method
Also have a look at resource controllers
Route::resource('categories', 'CategoryController'); could replace your entire routes definition
https://laravel.com/docs/5.5/controllers#resource-controllers
You're missing the back portion of the prefix on your categories group.
Route::group(['prefix' => 'back/categories'], function () {
That would match your routes.

Dynamic Route name Laravel 5.2

I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);

Laravel multiple roles on same route

This are currently my route implementation for user and auth roles on Laravel 5.1:
Route::group(['prefix' => 'admin', 'middleware' => 'auth:administrator'], function()
{
$a = 'admin.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'AdminController#getHome']);
});
Route::group(['prefix' => 'user', 'middleware' => 'auth:user'], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
I have another role where user can signup as merchant, but the issue is, how can I implement merchant route without duplicating the code, since both user and merchant using similar dashboard where merchant have extra features.
The implementation that currently worked is:
Route::group(['prefix' => 'user', 'middleware' => 'auth:merchant'], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
Thanks!!
You should be able to pass on a list of middlewares to your route using an array.
Route::group(['prefix' => 'user', 'middleware' => ['auth:user', 'auth:merchant']], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
However, I am not sure if this yields a result which does what you hope to achieve. Perhaps all this does is only allowing the route to users which belong to roles "user" AND "merchant", which probably isn't what you intend to do.

Laravel 4 how get routes by group name

In Laravel, I know I can get all routes using `Route::getRoutes() but I can't find if it is possible to get a list of all routes contained in a specified group.
For example, i have this route file:
Route::group(array('group_name' => 'pages'), function() {
Route::any('/authentication', array('as' => 'authentication', 'uses' => 'LogController#authForm' ));
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/index', array('as' => 'index-tags-categories', 'uses' => 'TagsCategoryController#index'));
Route::any('/tags_category/update', array('as' => 'update-tags-category', 'uses' => 'TagsCategoryController#update'));
});
});
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/store', array('as' => 'store-tags-category', 'uses' => 'TagsCategoryController#store'));
Route::any('/tags_category/update/{id}', array('as' => 'update-form-tags-category', 'uses' => 'TagsCategoryController#updateForm'));
Route::any('/tags_category/delete/{id}', array('as' => 'delete-tags-category', 'uses' => 'TagsCategoryController#delete'));
}); // operazioni protette
and in my controller i want obtain only routes contained in the first group (the one with the variable 'group_name').
Is it possible? If yes how I can do it? Thanks
The attributes passed to the group in the first parameter are stored on the route in the action array. This array can be accessed via the getAction() method on the route. So, once you get access to the route objects, you can filter based on this information.
$name = 'pages';
$routeCollection = Route::getRoutes(); // RouteCollection object
$routes = $routeCollection->getRoutes(); // array of route objects
$grouped_routes = array_filter($routes, function($route) use ($name) {
$action = $route->getAction();
if (isset($action['group_name'])) {
// for the first level groups, $action['group_name'] will be a string
// for nested groups, $action['group_name'] will be an array
if (is_array($action['group_name'])) {
return in_array($name, $action['group_name']);
} else {
return $action['group_name'] == $name;
}
}
return false;
});
// array containing the route objects in the 'pages' group
dd($grouped_routes);
User artisan to list all routes for the application
php artisan routes --name=admin
in Laravel 5
php artisan route:list --name=admin

Categories