Active State
, (*1)
Simple Laravel Active Checker For Request Url, (*2)
Sometimes you want to check the request url is active or not For the following purpose.
Especially for backend sidebar., (*3)
, (*4)
Basically we do like this., (*5)
<li class="sidebar {{ Request::is('post') ? 'active' : 'no' }} ">Post</li>
<li class="sidebar {{ Request::is('page') ? 'active' : 'no' }} ">Page</li>
It would be nice if we can make shorter. right ?, (*6)
<li class="sidebar {{ active_check('post') }} ">Post</li>
<li class="sidebar {{ active_check('page') }} ">Page</li>
Installation
Install with composer
:, (*7)
Laravel 5.4 and above, (*8)
composer require pyaesone17/active-state:^1.1.1
Laravel 5.3 and below, (*9)
composer require pyaesone17/active-state:^0.0.2
And add the service provider in config/app.php
, (*10)
'providers' => [
........,
Pyaesone17\ActiveState\ActiveStateServiceProvider::class,
]
If you want to use the facade, add this to your facades in config/app.php
, (*11)
'aliases' => [
........,
'Active' => Pyaesone17\ActiveState\ActiveFacade::class,
]
To publish configuration file, (*12)
php artisan vendor:publish --provider="Pyaesone17\ActiveState\ActiveStateServiceProvider"
Usage
It will check against whether your request is www.url.com/data
If the request match this url . It will return the default value from config file.
The default value for true state is "active"
and false is "no"
. You can configure on active.php ., (*13)
{{ Active::check('data') }}
To check the exact url., (*14)
{{ Active::check('data') }} // check request is www.url.com/data
To check the url deeply , just pass the true
value as second parameter., (*15)
{{ Active::check('data',true) }} // check request is www.url.com/data || www.url.com/data/*
OR, (*16)
{{ Active::check(['data','post','categories']) }} // check request is www.url.com/data || www.url.com/post || www.url.com/categories
```blade
{{ Active::check(['data','post','categories'],true) }} // check request is www.url.com/data/* || // check request is www.url.com/post/* || www.url.com/categories/*, (*17)
To change the return value in runtime, just pass the the third and fourth parameter.
```blade
{{ Active::check('data',true,'truth','fake') }} // it will override the value from config file.
Or you can even use helper function., (*18)
{{ active_check('data') }}
You can also use this package for conditional displaying data.
In some case, You need to render some part of template depends on request., (*19)
@ifActiveUrl('data')
Foo, (*20)
@else
Bar and Bazz, (*21)
@endIfActiveUrl
Advance Usage and Above version 1.1.1
To check the route name is., (*22)
{{ Active::checkRoute('users.index') }} // check request url route name is "users.index"
OR, (*23)
{{ Active::checkRoute(['users.index','users.show', 'users.edit']) }} // check request url route name is "users.index" or "users.show" or "users.edit"
Sometimes passing multiple routename as parameters is cubersome. You can simple do like this, (*24)
{{ Active::checkRoute('users.*') }}
Ofcousre you may change the return value in runtime as second and third params., (*25)
{{ Active::checkRoute('users.index','routeIsActive','routeNotActive') }}
OR, (*26)
{{ Active::checkRoute(['users.index','users.show'],'routeIsActive','routeNotActive') }}
For helper function., (*27)
{{ active_route('users.comments.*') }}
Yes it is also avaialable in blade., (*28)
@ifActiveRoute('users.index')
Foo, (*29)
@else
Bar and Bazz, (*30)
@endIfActiveRoute
To check the url with the exact same query paramter value., (*31)
{{ Active::checkQuery('users?gender=male') }} // check request is www.url.com/users?gender=male
OR, (*32)
{{ Active::checkQuery(['users?gender=male','users?status=married']) }} // check request is www.url.com/users?gender=male or www.url.com/users?status=married
Ofcousre you may change the return value in runtime as second and third params., (*33)
{{ Active::checkQuery(['users?gender=male','itIsMale','Ah it is wonder woman') }}
OR, (*34)
{{ Active::checkQuery(['users?gender=male','users?status=married'],'male or married','nothing') }}
For helper function., (*35)
{{ active_query('users?gender=male') }}
Yes it is also avaialable in blade., (*36)
@ifActiveQuery(['users?gender=male','users?status=married'])
<p>Foo</p>
@else
<p>Bar and Bazz</p>
@endIfActiveQuery
Notes
Everytime u update package, you have to run "php artisan view:clear" for blade directives., (*37)
Configuration
You can configure the return value of active state., (*38)
```php
return [, (*39)
// The default value if the request match given action
'active_state' => 'active',
// The default value if the request match given action
'inactive_state' => 'no'
];, (*40)