taffovelikoff/laravel-sef

为您的Laravel应用程序提供搜索引擎友好的URL。

v1.0.1 2020-03-03 21:46 UTC

This package is auto-updated.

Last update: 2024-09-04 08:34:57 UTC


README

🔗 为您的Laravel网站提供搜索引擎友好的URL。

内容

🤔 为什么使用它?

💻 要求

⚙️ 安装

📚 使用

为什么使用它?

创建搜索引擎友好的URL有很多种方法。例如,您可以使用“slug”

如果您想去掉前缀,而是有这个

这个包将帮助您实现这一点!

要求

本包需要 Laravel 5.8 或更高版本。

安装

您可以通过composer安装此包

composer require taffovelikoff/laravel-sef

不要忘记运行迁移。有一个名为 "sefs" 的迁移文件,其中将存储所有自定义URL。

php artisan migrate

使用

👉 第1步:将HasSef特性添加到模型

首先,您需要将 TaffoVelikoff\LaravelSef\Traits\HasSef 特性添加到您的模型中。

namespace App;

use TaffoVelikoff\LaravelSef\Traits\HasSef;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasSef;
}

👉 第2步:创建/更新SEF

namespace App\Http\Controllers\Admin;

use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    // Create a product
    public function store() {

        // Validate the request and make sure the "sef" keyword is unique.

        // Create
        $prod = Product::create([
            'name'  => 'My product',
            'price' => 2500
        ]);

        // Model to be available on https://mylaravel.com/my_product
        $product->createSef('my_product');

        return redirect()->back();
    }

    // Update a product
    public function update($id) {
        // Get the product
        $product = Product::findOrFail($id);

        // Update to be available on https://mylaravel.com/my_new_url
        $product->updateSef('my_new_url');

        return redirect()->back();
    }
}

现在您可以使用 sefUrl() 方法在模板中链接资源

<a href="{{ $product->sefUrl() }}">{{ $product->name }}</a>

👉 第3步:调用正确的控制器和操作

您有几种方式来调用用于查看模型的控制器和操作。

▶️ 方法1:配置文件中的URL映射。

发布配置文件

php artisan vendor:publish --tag:sef_config

将以下内容添加到您的路由文件(通常是web.php)的底部

Route::get('{keyword}', '\TaffoVelikoff\LaravelSef\Http\Controllers\SefController@viaConfig');

假设您正在尝试访问 https://mylaravel.com/something

如果 /something 在您的应用程序路由中没有定义,则将调用 SefController@viaConfig。此方法将在 "sefs" 表中查找关键字为 "something" 的记录。

如果不存在此类记录,将引发404错误。

如果找到记录,则方法将检查所有者模型类型(类)是否存在于配置/sef.php中的路由数组中

// config/sef.php

return [

    'routes' => [
        'App\Product' => [ // The owner model type
            'controller' => 'App\Http\Controllers\ProductController', // controller, that handles the request
            'method' => 'index' // the method to view (show) the model
        ],
    ]

];

▶️ 方法2:在模型中定义$sef_method属性

将以下内容添加到您的路由文件(通常是web.php)的底部

Route::get('{keyword}', '\TaffoVelikoff\LaravelSef\Http\Controllers\SefController@viaMethod');

假设您正在尝试访问 https://mylaravel.com/something

如果 /something 在您的应用程序路由中没有定义,则将调用 SefController@viaMethod。此方法将在 "sefs" 表中查找关键字为 "something" 的记录。

如果不存在此类记录,将引发404错误。

如果找到记录,则方法将检查所有者模型类型。例如,所有者模型类型为 "App\Product"。然后,方法将检查在 App\Product 模型中是否存在名为 $sef_method 的公共静态属性

namespace App;

use TaffoVelikoff\LaravelSef\Traits\HasSef;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasSef;

    // Controller@method to view/show the model.
    public static $sef_method = 'App\Http\Controllers\ProductController@index';
}

在这个例子中,"App\Http\Controllers\ProductController@index" 是用于查看/显示模型的控制器和操作。

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    // Show the model
    public function index($id) {
        // Get the product
        $prod = Product::findOrFail($id);

        // Display template
        return view('product');
    }
}

▶️ 方法3:您自己的控制器

将以下内容添加到您的路由文件(通常是web.php)的底部

Route::get('{keyword}', 'App\MySefController@redirect');

创建自己的控制器

namespace App\Http\Controllers;

use TaffoVelikoff\LaravelSef\Sef;
use App\Http\Controllers\Controller;

class SefController extends Controller
{

    // Redirect to right controller and method via the mapping in config
    public function redirect($keyword) {

        // Find SEF with keyword
        $sef = Sef::where('keyword', $request->route()->parameters['keyword'])->first();

        // Add your own code
        //return app()->call(..., ['id' => $sef->model_id]);
    }
}

许可证

本软件包是开源软件,许可协议为 MIT 许可。