GithubHelp home page GithubHelp logo

aon4o / laravel-page-titles Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 37 KB

A simple package that makes it easier to set page titles in a Laravel application.

Home Page: https://packagist.org/packages/aon2003/laravel-page-titles

License: MIT License

PHP 100.00%
laravel laravel-package page-title php php8 title

laravel-page-titles's Introduction

A simple package that makes it easier to set a page title in a Laravel application.

GitHub issues GitHub forks GitHub stars GitHub license Latest Version on Packagist GitHub Code Style Action Status Total Downloads

This simple package has only one purpose: compacting the job of setting page titles to a single file.

It makes available a global variable $page_title in all blade templates and views using a Middleware.

Support us

Buy Me A Coffee

Installation

You can install the package via composer:

composer require aon2003/laravel-page-titles

Next, you need to publish a config file and a language file:

php artisan vendor:publish --tag="laravel-page-titles"

Lastly, you need to add the custom middleware to your web's middleware group in your app/Http/Kernel.php file.

'web' => [
    ...
    \Aon2003\PageTitles\Middleware\PageTitles::class,
],

Usage

For the library to work all your routes MUST be named. After the installation a global variable $page_title will be available in all blade components. It will contain the name of the current route, which will be used to match a title that you can configure in the translation file: lang/en/page_titles.php.

By default, the page_titles.php file contains only two default page titles for route names 'index' and 'home'.

return [
    'index' => env('APP_NAME', 'Home Page'),
    'home' => env('APP_NAME', 'Home Page'),
];

You can add your custom titles by adding lines in this file like this:

return [
    ...
    '{route_name}' => '{page_title}',
];

Then, in your blade files you should add the following line so the titles are displayed:

<title>{{ __($page_title) }}</title>

Page titles with Variables

If you want to be able to pass variables to your page titles, you should pass them as an array to the view, add numbered placeholders to the title in the language files and change the <title> tag in the blade files:

page_titles.php:

return [
    ...
    'index' => 'Hello, :0!',
];

Controller:

function index() {
    $page_props = ['Alex'];
    
    return view('{page}', compact('page_props'));
}

{page}.blade.php:

<title>{{ __($page_title, $page_params ?? []) }}</title>

The resulting page title should be: Hello, Alex!

Translations file name

If you want, you could use different translation file name. The only thing you should do for it to work is to rename the translation files and change the translation_file_name variable in the page_titles.php config file to the file name you want to use.

page_titles.php:

return [
    /*
    |--------------------------------------------------------------------------
    | Translation File Name
    |--------------------------------------------------------------------------
    |
    | Used to find the page titles translations.
    | If you change the translation file name, you MUST change this variable, too!
    |
    */

    'translation_file_name' => 'page-titles',
];

Internationalization

If your project supports internationalization, you can simply add more {language_code}/page_title.php files to your lang folder. For more information check out the official Laravel docs.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-page-titles's People

Contributors

aon2003 avatar aon4o avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

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