1. Go to this page and download the library: Download sawastacks/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/ */
sawastacks / ci4-slugify example snippets
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\Post;
use SawaStacks\CodeIgniter\Slugify;
class TestController extends BaseController
{
public function index(){
$post = new Post();
$title = 'André & François won mathematics competion';
$slug = Slugify::table('posts')->make($title); //When you use table name
$slug = Slugify::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 SawaStacks\CodeIgniter\Slugify;
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 = Slugify::table('posts')->make($title);
$slug = Slugify::model(Post::class)->make($title);
/** When you specify slug field name. defaul is 'slug' */
$slug = Slugify::table('posts')->make($title,'post_slug');
$slug = Slugify::model(Post::class)->make($title,'post_slug');
/** When you specify divider/separator for generated slug. default is '-' */
$slug = Slugify::table('posts','id')->make($title);
$slug = Slugify::model(Post::class,'id')->make($title);
/** When you specify divider/separator for generated slug. default is '-' */
$slug = Slugify::table('posts')->separator('_')->make($title);
$slug = Slugify::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 SawaStacks\CodeIgniter\Slugify;
class TestController extends BaseController
{
public function update_post(){
$id = $request->getVar('post_id');
$post = new Post();
$title = 'André & François won mathematics competion';
$slug = Slugify::model(Post::class)->sid($id)->make($title);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.