如何优雅的去设置每个页面的 SEO 信息?

目前我的解决方法

<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\View\View;

class Controller extends BaseController
{
    protected $page_title = null;

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function view(string $view = null, array $data = [], array $mergeData = []): View
    {
        if ($this->page_title) {
            \view()->share('page_title', $this->page_title);
        }
        return \view($view, $data, $mergeData);
    }
}

在默认的方法重写 view 方法,然后在调用 view helper

thanks.
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 4

protected $page_title = null;

string 类型的默认值,设置成 '' 会不会更好点

5年前 评论

ViewComposer 了解一下

5年前 评论

在 AppservideProvider 中的 boot 方法中使用 view::share 方法岂不是更好

View::share(['page_title'=>$this->page_title]);
5年前 评论
StringKe (楼主) 5年前