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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助