What is SEO Friendly URLs in codeigniter?

How to create seo firendly url in codeigniter

File Name :

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

class Posts extends CI_Controller {

function __construct() {
parent::__construct();
$this->load->model('Auth_model');
$this->load->helper('Common');
$this->load->library('form_validation');
}

public function index(){
$data = array();

//get the posts data
$data['posts'] = $this->Auth_model->getRows(array('limit'=>10));

//load the view
$this->load->view('post_view', $data);

}

public function details($url_slug){

//echo $url_slug;
//exit;
$data = array();

//get the post data
$data['post'] = $this->Auth_model->getRows(array('url_slug'=>$url_slug));

//load the view
$this->load->view('details', $data);
}

public function add(){
$data = array();
$postData = array();
if($this->input->post('submitBtn')){
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');

$postData = array(
'title' => strip_tags($this->input->post('title')),
'content' => strip_tags($this->input->post('content'))
);

if($this->form_validation->run() == true) {
/*
* Generate SEO friendly URL
*/
$title = strip_tags($this->input->post('title'));
$titleURL = strtolower(url_title($title));
if(isUrlExists('posts',$titleURL)){
$titleURL = $titleURL.'-'.time();
}
$postData['url_slug'] = $titleURL;

//Insert post data to database
$insert = $this->Auth_model->insert($postData);
if($insert){
$postData = array();
$data['success_msg'] = 'Post data inserted successfully.';
} else {
$data['error_msg'] = 'Some problems occured, please try again.';
}
}
}

$data['post'] = $postData;

//load the view
$this->load->view('add', $data, false);
}
}

post_view

<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">
<h1>Post List</h1>
<div class="row">
<ul class="post-list">
<?php if(!empty($posts)): foreach($posts as $post): ?>
<li><a href="<?php echo base_url($post['url_slug']); ?>"><?php echo $post['title']; ?></a></li>
<?php endforeach; else: ?>
<p>Post(s) not available.</p>
<?php endif; ?>
</ul>
</div>
</div>

</body>
</html>

add

<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">
<h1>Add Post</h1>
<div class="row">
<?php echo !empty($success_msg)?'<p>'.$success_msg.'</p>':''; ?>
<?php echo !empty($error_msg)?'<p>'.$error_msg.'</p>':''; ?>

<form method="post" action="<?php echo base_url()?>posts/add">
<p>Post Title: <input type="text" name="title" value="<?php echo !empty($post['title'])?$post['title']:''; ?>"></p>
<?php echo form_error('title','<p class="help-block error">','</p>'); ?>

<p>Post Content: <textarea name="content"><?php echo !empty($post['content'])?$post['content']:''; ?></textarea></p>
<?php echo form_error('content','<p class="help-block error">','</p>'); ?>

<input type="submit" name="submitBtn" value="Add">
</form>
</div>
</div>
</body>
</html>

details

<!DOCTYPE html>
<html lang="en">
<head>
<title>Generate SEO Friendly URL in CodeIgniter</title>
</head>
<body>
<div class="container">

<?php echo "hello url";?>

<h1><?php echo $post['title']; ?></h1>
<p><?php echo $post['content']; ?></p>
</div>
</body>
</html>

Add

<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Add extends CI_Controller
{
public function __construct()
{
parent::__construct();
}

public function index()
{
$this->load->view('add');
}
}

routes

$route['default_controller'] = 'posts';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['(:any)'] = 'posts/details/$1';
$route['posts/(:any)'] = 'posts/details/$1';


Auth_model

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

class Auth_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}


function getRows($params = array()){
$this->db->select('*');
$this->db->from('posts');

//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}

if(array_key_exists("url_slug", $params)){
$this->db->where('url_slug', $params['url_slug']);
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->row_array():FALSE;
}else{
$query = $this->db->get();
$result = ($query->num_rows() > 0)?$query->result_array():FALSE;
}

//return fetched data
return $result;
}

/*
* Insert post data to table
*/
public function insert($data = array()) {
if(!array_key_exists("created", $data)){
$data['created'] = date("Y-m-d H:i:s");
}
if(!array_key_exists("modified", $data)){
$data['modified'] = date("Y-m-d H:i:s");
}

//insert data
$insert = $this->db->insert('posts', $data);
if($insert){
return $this->db->insert_id();;
}else{
return false;
}
}



}

--
-- Table structure for table `posts`
--

CREATE TABLE `posts` (
`id` int(11) NOT NULL,
`url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=Active, 0=Inactive'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `posts`
--

INSERT INTO `posts` (`id`, `url_slug`, `title`, `content`, `created`, `modified`, `status`) VALUES
(1, 'socha-nahi-thaa-takdeer-yahan-layegi', 'socha nahi thaa', 'lovely song', '2019-02-16 00:00:00', '2019-02-16 00:00:00', 1),
(2, 'kabhi-kabhi-kabhi-gum', 'kabhi', 'hjgh gjhkj hj jjh', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 1);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `posts`
--
ALTER TABLE `posts`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `posts`
--
ALTER TABLE `posts`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here