dregduc63699 2014-12-26 12:37
浏览 93
已采纳

Laravel检测视图中的路径组

In my admin pages, I want to manage my ecommerce products using AngularJS.

e.g. admin/product which will query an api in admin/api/product

I have not yet set up user authentication so I dont yet know if the user is an admin user or not.

I only wish to include angularjs admin scripts on admin pages.

Is there a way I can include an angular adminapp.js in my view only if the route group is admin. e.g. for public facing pages, I don't expose the adminapp.js to public facing pages.

I know I can do this if the user is authenticated as admin - but I wish to be able to do this 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 in the 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 above example is that if I am in a deep nested view: admin/categories/products then my prefix will no longer be admin. I don't want to go down the route of using a regex to detect the word admin in the route prefix.

  • 写回答

1条回答 默认 最新

  • doutiaoku4495 2014-12-26 12:55
    关注

    There's no built in way that I know of, but here's something that works:

    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 at the top of the routes file to make sure the $routeGroup variable is always set:

    View::share('routeGroup', null);
    

    Then in your view:

    @if($routeGroup == 'admin')
        {{ HTML::script('js/adminapp.js') }}
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?