Dear Mates, Please click the ads to support us.
Tutorial

How to create slug in codeigniter

Kailash Singh Nov 01, 2020 12208 Views
Kailash Singh

How to create slug in codeigniter Share

Published Nov 01, 2020 by Kailash Singh 0 Comments 12208 Views

In this tutorial, we are going to create how to create slug in codeigniter.

 

A slug is a part of the URL when you are accessing a resource. Say you have a URL, such as the one below, that displays all of the posts in the system:

 

For  eg: 

http://localhost/codeigniter/view/1/how-to-upload-file-using-javascript-and-php

 

Step 1 :- Create post table in database.

 

CREATE TABLE `elevenstech`.`posts` ( `id` INT NOT NULL AUTO_INCREMENT , `title` TEXT NOT NULL , `description` LONGTEXT NOT NULL , `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB;

 

Step 2 :- Create Home Controller ( Home.php ).

 

In this conttoller. We are  creating view page to load all posts.

 

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
	
	public function index()
	{
        //load all post from posts table
		$lists = $this->db->select('*')->from('posts')->get()->result();

		$this->load->view('home',['lists'=>$lists]);
	}

}

?>

 

Step 3 :- Create View Page in View Folder ( home.php ).

 

In this view page. We are  loading all posts with the help of foreach loop.

 

<?php foreach ($lists as $list) { ?>

	 <div class="box_border">

	 	<h4><?php echo $list->title; ?></h4>

	 	<a href="#">View</a>

	 </div>

<?php } ?>

 

Step 4 :- Create Slug in helper Folder ( site_helper.php ).

 

In this helper file. We are  creating a slug function.

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function slug($string, $spaceRepl = "-")
{
    $string = str_replace("&", "and", $string);

    $string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);

    $string = strtolower($string);

    $string = preg_replace("/[ ]+/", " ", $string);

    $string = str_replace(" ", $spaceRepl, $string);

    return $string;
}

 

Step 5 :- Define slug file in home Controller ( Home.php ).

 

In this controlller. We are  calling slug function in contructor so that i can use this function on the view page.

 

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	public function __construct(){
		parent::__construct();

		$this->load->helper('site_helper'); // load helper file
	}

	
	public function index()
	{
		$lists = $this->db->select('*')->from('slug')->get()->result();
		$this->load->view('home',['lists'=>$lists]);
	}

}

 

Step 5 :- Create slug in anchor tag ( home.php ).

 

<?php foreach ($lists as $list) { ?>

	 <div class="box_border">

	 	<h4><?php echo $list->title; ?></h4>

        // here i am using slug
	 	<a href="<?php echo base_url().'view/'.$list->id.'/'.slug($list->title) ?>">View</a>

	 </div>

<?php } ?>

 

Step 6 :- Now use routing for view page.

 

$route['view/:num/(:any)'] = 'home/view/$1';

 

Step 7 :- Create view page for view post.

 

public function view()
{

    //here i getting id from the url
	$id = $this->uri->segment(2);

	$lists = $this->db->select('*')->from('slug')->where('id',$id)->get()->row();

	$this->load->view('view',['list'=>$lists]);

}

 

Step 8 :- Create view page to view post ( view.php ).

 

<?php 

      echo $list->title; 

      echo $list->description; 

?>

 

Result  :- 

 

Comments 0


SEARCH POST HERE


Support Us

Subscribe My YouTube Channel

Join Our Telegram Channel & Support Eachother

CATEGORIES

INTERVIEW QUESTIONS

PROJECT SOURCE CODE






POPULAR POSTS