rephlux/pagetitle

管理Laravel视图中的页面标题

v2.4 2020-04-09 07:00 UTC

This package is auto-updated.

Last update: 2024-09-09 16:28:02 UTC


README

Build Status Latest Stable Version Scrutinizer Code Quality License

通常,你会发现自己处于想要更多控制不同视图页面标题设置的情况。虽然在主视图中可以返回页面标题,但是处理格式(如分隔符使用或附加/预置默认页面标题)可能会很麻烦。

此包简化了此过程。

安装

首先通过Composer安装此包

composer require rephlux/pagetitle

Laravel用户

如果您是Laravel用户,则需要将服务提供器添加到您的config/app.php文件中。

'providers' => [
    '...',
    Rephlux\PageTitle\PageTitleServiceProvider::class
];

此包还提供了一个外观,如果您想在控制器和视图中使用外观,也可以将其注册到您的config/app.php中。

 'aliases' => [
     '...'
     'PageTitle' => Rephlux\PageTitle\Facades\PageTitle::class,
 ]

在Laravel 5.5中,服务提供器和别名会自动注册。如果您使用Laravel 5.5,则可以跳过这些步骤。

版本2.0

如果您仍在使用PHP 7.0或5.6,则需要使用此包的1.0版本。2.0版及以上仅与PHP 7.1或更高版本兼容。

此包的2.0版也兼容Laravel 5.5及更高版本。

用法

要简单地添加单个页面标题,请调用相应的add()方法,并传递一个字符串作为参数

public function index()
{
    \PageTitle::add('Welcome to our Homepage');

    return view('hello');
}

您还可以使用全局的pagetitle辅助函数。

public function index()
{
    pagetitle('Welcome to our Homepage');

    return view('hello');
}

要一次添加多个页面标题部分,只需传递一个数组作为参数。

public function index()
{
    pagetitle([
        'About us',
        'Profile'
    ]);

    return view('hello');
}

将pagetitle添加到blade视图中

现在您可以在视图中显示完全连接的页面标题。最佳方式是在主布局文件中使用它。

<head>
    <meta charset="UTF-8">
    <title>{{ pagetitle()->get() }}</title>
    ...
</head>

要按相反顺序显示完全连接的页面标题,只需传递reverse参数。

<head>
    <meta charset="UTF-8">
    <title>{{ pagetitle()->get('reverse') }}</title>
    ...
</head>

downward模式下,首先按相反顺序连接所有页面标题部分,然后附加页面名称(如果已设置选项

<head>
    <meta charset="UTF-8">
    <title>{{ pagetitle()->get('downward') }}</title>
    ...
</head>

默认值

如果您使用Laravel,有三个配置选项您需要关注。首先,使用以下命令发布默认配置

php artisan vendor:publish --provider="Rephlux\PageTitle\PageTitleServiceProvider"

这将向以下位置添加一个新的配置文件:config/pagetitle.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Page name
    |--------------------------------------------------------------------------
    |
    | Type your page name for your website.
    | This will be used when there are more titles to concatenate with.
    |
    */
    'page_name' => '',

    /*
    |--------------------------------------------------------------------------
    | Default title when empty
    |--------------------------------------------------------------------------
    |
    | This will be used when therer is no other title.
    | Mainly used for the home page of your website.
    |
    */
    'default_title_when_empty' => '',

    /*
    |--------------------------------------------------------------------------
    | Delimiter
    |--------------------------------------------------------------------------
    |
    | Titles will be concatenated using this delimiter.
    |
    */
    'delimiter' => ' :: ',
];

page_name

如果您想,可以将页面名称输入到该密钥中,以将名称附加/预置到连接的页面标题中。

default_title_when_empty

当集合中没有页面标题部分时,将使用此文本。

delimiter

当您想使用分隔符时,只需更新此键并添加您想使用的分隔符字符串。

更改配置值

每个配置参数都可以通过页面标题对象实例的相应setter方法进行更改

  • setDelimeter(delimeter)
  • setPageName(pageName)
  • setDefault(default)

示例用法

public function index()
{
    pagetitle('My Blog Post')->setPageName('My Blog')->setDelimeter(' / ');

    return view('hello');
}

要获取当前配置值,请在页面标题对象实例上使用相应的getter方法

  • getDelimeter()
  • getPageName()
  • getDefault()

许可证

查看此存储库的许可证