PHP code example of mberecall / ci4-slugify

1. Go to this page and download the library: Download mberecall/ci4-slugify 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/ */

    

mberecall / ci4-slugify example snippets




namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Post;
use \Mberecall\CI_Slugify\SlugService;

class TestController extends BaseController
{
    public function index(){
        $post = new Post();
        $title = 'André & François won mathematics competion';
        $slug = SlugService::table('posts')->make($title); //When you use table name
        $slug = SlugService::model(Post::class)->make($title); //When you use model object
        //Result for $slug: andre-francois-won-mathematics-competion-3
        $post->save([
            'title'=>$title,
            'slug'=>$slug 
        ]);
    }   
}



namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Post;
use \Mberecall\CI_Slugify\SlugService;

class TestController extends BaseController
{
    public function index(){

        $post = new Post();
        $title = 'André & François won mathematics competion';

        /** Default way. Both lines will return same result */
        $slug = SlugService::table('posts')->make($title);
        $slug = SlugService::model(Post::class)->make($title);

        /** When you specify slug field name. defaul is 'slug' */
        $slug = SlugService::table('posts')->make($title,'post_slug');
        $slug = SlugService::model(Post::class)->make($title,'post_slug');

        /** When you specify divider/separator for generated slug. default is '-' */
        $slug = SlugService::table('posts','id')->make($title);
        $slug = SlugService::model(Post::class,'id')->make($title);
    
       /** When you specify divider/separator for generated slug. default is '-' */
        $slug = SlugService::table('posts')->separator('_')->make($title);
        $slug = SlugService::model(Post::class)->separator('_')->make($title);
            
        //Result for $slug: 1: andre-francois-won-mathematics-competion
        //Result for $slug: 2: andre_francois_won_mathematics_competion   
    }   
}




namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Post;
use \Mberecall\CI_Slugify\SlugService;

class TestController extends BaseController
{
    public function update_post(){
        $id = $request->getVar('post_id');
        $post = new Post();
        $title = 'André & François won mathematics competion';

        $slug = SlugService::model(Post::class)->sid($id)->make($title);
  
    }

}