hypnotract/laravel-view-identifiers

A quick way to set global classes and global id to main HTML DOM element in Laravel 5.

This package's canonical repository appears to be gone and the package has been frozen as a result.

5.0.0 2018-06-09 23:48 UTC

This package is not auto-updated.

Last update: 2020-06-07 06:31:20 UTC


README

A quick way to set view identifiers to main HTML DOM element in Laravel 5. Let's call those identifiers: global classes and global id.

Documentation

Possibilities

This package gives you the possibility:

  • to set identifiers on main HTML elements depending on some backend criteria
  • to easily detect in JavaScript or CSS what page is loaded
  • to make DOM manipulations in JavaScript only on certain set of pages, for example on all Admin Panel pages
  • to set some styles only on certain set of pages, for example on all Article View pages, but not in Admin Panel

etc.

Installation

For older versions of Laravel (not tested yet) see: Laravel 5.0-5.4 and Laravel 5.5.

composer require hypnotract/laravel-view-identifiers

Configuration

You can publish the config file to change the default settings.

php artisan vendor:publish --provider="Hypnotract\ViewIdentifier\ServiceProvider" --tag=config

Converting Styles

Supported Values and Examples

kebab converts strings into "kebab-case"

snake converts strings into "snake_case"

camel converts strings into "camelCase"

studly converts strings into "StudlyCase"

Auto Discovery

By default, auto discovery is enabled. It means that classes and id will be set up for you, depending on controller and action name.

Controllers Namespace

If your controllers namespace differs from the default one App\Http\Controllers, you can set your own namespace.

Usage

Defaults

By default, classes and id auto discovery is enabled, and you can set these to the needed HTML elements right away.

<div class="content {{ ViewIdentifier::class()->get() }}">
<html id="{{ ViewIdentifier::id()->get() }}" lang="en">
<body id="{{ ViewIdentifier::id()->get() }}" class="{{ ViewIdentifier::class()->get() }}">

To disable auto discovery, see Configure Auto Discovery.

Examples

App\Http\Controllers\Web\PageController::show will get the id web-page-show and the class: web page show.

App\Http\Controllers\Admin\PageController::edit will get the id admin-page-edit and the class: admin page edit.

App\Http\Controllers\ArticleController::view will get the id article-view and the class: article view.

Own Identifiers

If you want to set your own or any additional identifiers, you can do it like so:

\ViewIdentifier::id()->set('whateverId');

\ViewIdentifier::class()->push('whateverClass');
\ViewIdentifier::class()->push('whateverSecondClass');

You can pass strings in any case style you want, as those will be converted to the styles set in configuration file.

Examples
use ViewIdentifier;

class AdminController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        ViewIdentifier::class()->push('adminPanel');
    }
}

class ArticlesController extends AdminController
{
    public function __construct()
    {
        parent::__construct();

        ViewIdentifier::class()->push('article');
    }
    
    public function index()
    {
        ViewIdentifier::class()->push('index');
        
        $articles = Article::index()->paginate(25);
    
        return View::make('admin.articles.index')->with(compact('articles'));
    }
}

And the result class in ViewIdentifier::class()->get() will be: admin-panel article index.

If You Need Help

Please submit all issues and questions using GitHub issues and I will try to help you.

Credits

License

Laravel View Identifiers is free software distributed under the terms of the MIT license.