PHP code example of taffovelikoff / laravel-sef

1. Go to this page and download the library: Download taffovelikoff/laravel-sef library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

taffovelikoff / laravel-sef example snippets


namespace App;

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

class Product extends Model
{
    use HasSef;
}

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


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

// 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
        ],
    ]

];

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

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


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


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]);
    }
}
bash
php artisan migrate
bash
php artisan vendor:publish --tag:sef_config