Code Monkey home page Code Monkey logo

active-state's Introduction

Active State

Total Downloads License Latest Stable Version Monthly Downloads

Simple Laravel Active Checker For Request Url

Sometimes you want to check the request url is active or not For the following purpose. Especially for backend sidebar.

Bilby Stampede

Basically we do like this.

<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 ?

<li class="sidebar {{ active_check('post') }} ">Post</li>
<li class="sidebar {{ active_check('page') }} ">Page</li>

Installation

Install with composer:

Laravel 5.4 and above

composer require pyaesone17/active-state:^1.1.1

Laravel 5.3 and below

composer require pyaesone17/active-state:^0.0.2

And add the service provider in config/app.php

'providers' => [
    ........,
    Pyaesone17\ActiveState\ActiveStateServiceProvider::class,
]

If you want to use the facade, add this to your facades in config/app.php

'aliases' => [
    ........,
    'Active' => Pyaesone17\ActiveState\ActiveFacade::class,
]

To publish configuration file

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 .

{{ Active::check('data') }} 

To check the exact url.

{{ Active::check('data') }} // check request is www.url.com/data

To check the url deeply , just pass the true value as second parameter.

{{ Active::check('data',true) }}  // check request is www.url.com/data || www.url.com/data/*

OR

{{ Active::check(['data','post','categories']) }}  // check request is www.url.com/data || www.url.com/post || www.url.com/categories
{{ Active::check(['data','post','categories'],true) }} // check request is www.url.com/data/* || // check request is www.url.com/post/* || www.url.com/categories/*

To change the return value in runtime, just pass the the third and fourth parameter.

{{ Active::check('data',true,'truth','fake') }} // it will override the value from config file.

Or you can even use helper function.

{{ 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.

@ifActiveUrl('data')
    <p>Foo</p>
@else
    <p>Bar and Bazz</p>
@endIfActiveUrl

Advance Usage and Above version 1.1.1

To check the route name is.

{{ Active::checkRoute('users.index') }} // check request url route name is "users.index"

OR

{{ 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

{{ Active::checkRoute('users.*') }} 

Ofcousre you may change the return value in runtime as second and third params.

{{ Active::checkRoute('users.index','routeIsActive','routeNotActive') }} 

OR

{{ Active::checkRoute(['users.index','users.show'],'routeIsActive','routeNotActive') }} 

For helper function.

{{ active_route('users.comments.*') }} 

Yes it is also avaialable in blade.

@ifActiveRoute('users.index')
    <p>Foo</p>
@else
    <p>Bar and Bazz</p>
@endIfActiveRoute

To check the url with the exact same query paramter value.

{{ Active::checkQuery('users?gender=male') }} // check request is www.url.com/users?gender=male

OR

{{ 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.

{{ Active::checkQuery(['users?gender=male','itIsMale','Ah it is wonder woman') }} 

OR

{{ Active::checkQuery(['users?gender=male','users?status=married'],'male or married','nothing') }} 

For helper function.

{{ active_query('users?gender=male') }} 

Yes it is also avaialable in blade.

@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.

Configuration

You can configure the return value of active state.

return [

    // The default  value if the request match given action
    'active_state'      =>  'active',

    // The default  value if the request match given action
    'inactive_state'    =>  'no'

];

active-state's People

Contributors

garbinmarcelo avatar llaski avatar nayzawoo avatar nyanwin avatar pyaesone17 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.