Laravel Detect Route Group in View
In my admin pages, I want to manage my ecommerce products using AngularJS.
eg. admin / product which will query the api in admin / api / product
I haven't configured user authentication yet, so I don't know yet if the user is an administrator or not.
I only want to enable angularjs admin scripts in admin pages.
Is there a way to include angular adminapp.js in my opinion only if the route group is admin. for example for public pages, I don't expose adminapp.js for public pages.
I know I can do this if the user is authenticated as admin, but I want it to be possible if the route group is admin.
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
Route::group(['prefix' => 'api', 'namespace' => 'Api'], function() {
Route::resource('product', 'ProductController');
});
Route::group(['namespace' => 'Product'], function() {
Route::get('product', 'ProductController');
});
});
And into templates.master.blade.php
something like:
@if($routeGroupIsAdmin)
{{ HTML::script('js/adminapp.js') }}
@endif
or even:
{{ Route::getCurrentRoute()->getPrefix() == 'admin'? HTML::script('js/adminapp.js') : HTML::script('js/app.js') }}
But the problem with the above example is that if I am in a deep nested view: admin / categories / products then my prefix is no longer admin. I don't want to go down the route of using a regex to detect a word admin
in the route prefix.
source to share
There's no built-in way I know of, but something works here:
First add a route filter
Route::filter('set-route-group', function($route, $request, $value){
View::share('routeGroup', $value);
});
Then add this to your admin group (you can also use it for other groups in the future):
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'set-route-group:admin'], function(){
Also add this to the top of the file routes
so that the variable is always set $routeGroup
:
View::share('routeGroup', null);
Then, in your opinion:
@if($routeGroup == 'admin')
{{ HTML::script('js/adminapp.js') }}
@endif
source to share