knash94/seo

为Laravel 5+提供404页面记录和重定向功能

v1.2.2 2019-04-22 09:14 UTC

README

Total Downloads Latest Stable Version License Build Status

A laravel 5 package that adds the ability to log 404 errors and to action these with redirects via a user interface. This has several benefits over creating redirect in a .htaccess (Or your web servers equivalent) as the main laravel application will run and then 404 if a page cannot be found, this is where the package will determine whether there is a redirect available. This prevents scenarios where a redirect may be hit before laravel and limiting access to your application. On top of this, it gives SEO members the ability to track 404 errors, tracking where they have come from and then action them with ease.

安装

使用composer安装包

composer require knash94\seo

安装完成后,将以下行添加到您的 config/app.php 服务提供者中

'Knash94\Seo\SeoServiceProvider'

然后打开您的 \App\Exceptions\Handler.php 文件,在render方法下插入以下代码,确保不要更改方法上的任何现有代码

$redirect = $this->reportNotFound($exception);

if ($redirect && $redirect instanceof RedirectResponse) {
    return $redirect;
}

同时将 LogsMissingPages 特性添加到 \App\Exceptions\Handler.php

use LogsMissingPages;

同时导入 Illuminate\Http\RedirectResponse;App\Exceptions\Handler.php

最后,通过运行以下命令发布供应商文件并运行迁移

php artisan vendor:publish --provider=Knash94\Seo\SeoServiceProvider
php artisan migrate

配置

有多种配置选项可供选择,我建议设置 middlewaretemplatesection 值。只需打开seo-tools.php并设置新值。以下是一个使用您自己的模板的示例设置

<?php

return [
    'routing' => [
        'prefix' => 'admin/seo-tools',
        'namespace' => 'Knash94\Seo\Http\Controllers',
        'middleware' => ['auth']
    ],

    'views' => [
        'template' => 'layout.admin',
        'section' => 'content',
        'index' => 'seo-tools::bootstrap3.index',

        'errors' => [
                    'edit' => 'seo-tools::bootstrap3.errors.edit',
                    'view' => 'seo-tools::bootstrap3.errors.view'
                ],
        
                'redirects' => [
                    'edit' => 'seo-tools::bootstrap3.redirects.edit',
                    'delete' => 'seo-tools::bootstrap3.redirects.delete'
                ]
        ],
        
        // Filters what URLs to not record using regex
        'filters' => [
            '/(\.png)|(\.jpg)|(\.gif)/',
            '/wp\-login\.php/'
        ]
];

如果您想将重定向管理器链接到您的管理面板,请创建一个指向 seo-tools.index 路由的链接

待办事项

  • 测试套件
  • 在应用更改之前测试重定向是否按预期工作
  • 批量插入重定向
  • 在重定向中添加正则表达式的能力