定制 taffovelikoff/laravel-sef 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

taffovelikoff/laravel-sef

Composer 安装命令:

composer require taffovelikoff/laravel-sef

包简介

Search engine friendly URLs for your laravel app.

README 文档

README

🔗 Search engine friendly urls for your Laravel website.

Contents

🤔 Why use it?

💻 Requirements

⚙️ Installation

📚 Usage

Why use it?

There are many ways to create search engine friendly urls. For example you can use "slugs":

What if you want to loose the prefix and have this instead:

This package will help you achieve that!

Requirements

This package requires Laravel 5.8 or above.

Installation

You can install the package via composer:

composer require taffovelikoff/laravel-sef

Don't forget to run the migrations. There is a migration file for a "sefs" table, where all the custom urls will be stored.

php artisan migrate

Usage

👉 STEP 1: Add the HasSef trait to a model

First, you need to add the TaffoVelikoff\LaravelSef\Traits\HasSef trait to your model.

namespace App;

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

class Product extends Model
{
    use HasSef;
}

👉 STEP 2: Create/update the 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();
    }
}

Now you can use the sefUrl() method to link the resource in your templates:

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

👉 STEP 3: Call the right controller and method

You have a few options on how to call the controller and method used to view the model.

▶️ Method 1: URL mappings in config file.

Publish the configuration file:

php artisan vendor:publish --tag:sef_config

Add this to your routes file (typically web.php) at the VERY BOTTOM.

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

Say you are trying to reach https://mylaravel.com/something.

If /something is not defined in your app routes SefController@viaConfig will be called. This method will search in the "sefs" table for a record where keyword = 'something'.

If no such record exists a 404 error will be thrown.

If the record is found the method will check if the owner model type (class) exists in the routes array in config/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
        ],
    ]

];

▶️ Method 2: Define a $sef_method property in the model

Add this to your routes file (typically web.php) at the VERY BOTTOM.

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

Say you are trying to reach https://mylaravel.com/something.

If /something is not defined in your app routes SefController@viaMethod will be called. This method will search in the "sefs" table for a record where keyword = 'something'.

If no such record exists a 404 error will be thrown.

If the record is found the method will next check what is the owner model type. Say the owner model is of type "App\Product". Next the method will check for a public static property $sef_method in the App\Product model:

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';
}

In this example "App\Http\Controllers\ProductController@index" is the controller and method used to view/show the model.

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');
    }
}

▶️ Method 3: Your own controller

Add this to your routes file (typically web.php) at the VERY BOTTOM.

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

Create your own controller:

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]);
    }
}

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 241
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固