Ad

Check Request URL In Laravel

- 1 answer

In dashboard.blade.php, I manage js library links.

So, as I don't want to include all libs in all pages, I do it like that:

@if (Request::is("mail/create"))
        {!! Html::script('js/plugins/ui/nicescroll.min.js') !!}
        {!! Html::script('js/sidebar_detached_sticky_custom.js') !!}
@endif

for each page or group of pages.

Thing is when I edit an item, I my URL have this format:

http://laravel.dev:8000/mail/1/edit

And as 1 is the shop Id and vary, my method doesn't work anymore....

Any way to fix my issue or to do it better???

Tx!

Ad

Answer

You can use a route name instead, then you can do it like this:

@if (Request::route()->getName() == "mail_edit") {
    {!! Html::script('js/plugins/ui/nicescroll.min.js') !!}
    {!! Html::script('js/sidebar_detached_sticky_custom.js') !!}
}

Here's the laravel docs for named routes: https://laravel.com/docs/5.1/routing#named-routes

Or you can do this:<1/routing#named-routes

Or you can do this:

@if (strpos(Request::url(),'mail') !== false && strpos(Request::url(),'edit') !== false) {
    {!! Html::script('js/plugins/ui/nicescroll.min.js') !!}
    {!! Html::script('js/sidebar_detached_sticky_custom.js') !!}
}
Ad
source: stackoverflow.com
Ad