How to Create a SEO Friendly URL in CodeIgniter
Submitted by nurhodelta_17 on Saturday, April 7, 2018 - 14:21.
Installing CodeIgniter
If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter. Note: I'm using version 3.1.7 in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as ci_seo_url so I'm using the below code.- localhost/ci_seo_url
Removing index.php in our URLs
By default, index.php is included in every URL of our app. To remove this, please refer to my tutorial How to Remove Index.Php in the URL of Codeigniter Application.Creating our Database
I've included a .sql file located in db folder in the downloadable of this tutorial. All you need to do is import the file into your phpMyAdmin. If you have no idea how to import, please refer to my tutorial How import .sql file to restore MySQL database. You should be able to add a database named db.Connecting our App into our Database
Next, we're going to connect our codeigniter application to the database that we created earlier. 1. In your codeigniter app folder, open database.php located in application/config folder. 2. Update database.php with your credential the same as what I did below.- 'dsn' => '',
- 'hostname' => 'localhost',
- 'username' => 'root',
- 'password' => '',
- 'database' => 'db',
- 'dbdriver' => 'mysqli',
- 'dbprefix' => '',
- 'pconnect' => FALSE,
- 'db_debug' => (ENVIRONMENT !== 'production'),
- 'cache_on' => FALSE,
- 'cachedir' => '',
- 'char_set' => 'utf8',
- 'dbcollat' => 'utf8_general_ci',
- 'swap_pre' => '',
- 'encrypt' => FALSE,
- 'compress' => FALSE,
- 'stricton' => FALSE,
- 'save_queries' => TRUE
- );
Creating our Model
Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion. Create a file named Posts_model.php in application/models folder of our app and put the ff codes:- <?php
- class Posts_model extends CI_Model {
- public function __construct(){
- parent::__construct();
- $this->load->database();
- }
- public function fetchAllPost(){
- $query = $this->db->get('posts');
- return $query->result();
- }
- public function insertPost($data){
- return $this->db->insert('posts', $data);
- }
- public function getPost($slug){
- return $query->row_array();
- }
- }
Creating our Controller
Next step is to create our controller. Controllers follow the same naming convention as models. Create a file named Posts.php in application/controllers folder of our app and put the ff codes.- <?php
- class Posts extends CI_Controller {
- public function __construct(){
- parent::__construct();
- $this->load->helper('url');
- $this->load->model('posts_model');
- $this->load->library('session');
- }
- public function index(){
- $data['posts'] = $this->posts_model->fetchAllPost();
- $this->load->view('show_post', $data);
- }
- public function addPost(){
- $posts['title'] = $this->input->post('post_title');
- $posts['post_text'] = $this->input->post('post_text');
- //create slug
- $posts['post_slug'] = $this->slugify($posts['title']);
- //insert post
- $query = $this->posts_model->insertPost($posts);
- if($query){
- redirect('/');
- }
- }
- public function slugify($string){
- //remove prepositions
- $preps = array('in', 'at', 'on', 'by', 'into', 'off', 'onto', 'from', 'to', 'with', 'a', 'an', 'the');
- // replace non letter or digits by -
- // trim
- // transliterate
- // lowercase
- // remove unwanted characters
- return 'n-a';
- }
- return $string;
- }
- public function view($slug){
- $data['post_row'] = $this->posts_model->getPost($slug);
- $this->load->view('view_post', $data);
- }
- }
Creating our Routes
Next, we are going to set up the routes of our application. Open routes.php located in application/config folder and update it with the codes below to setup our default route and post route.- $route['default_controller'] = 'posts';
- $route['404_override'] = '';
- $route['translate_uri_dashes'] = FALSE;
- //view post
- $route['post/(:any)'] = 'posts/view/$1';
Creating our Views
Lastly, we create the views of our app. Take note that I've use Bootstrap in the views which is included in the downloadables of this tutorial. But if you want, you may download bootstrap using this link. Create the ff files inside application/views folder. show_post.php- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>How to Create a SEO Friendly URL in CodeIgniter</title>
- <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">Create a SEO Friendly URL in CodeIgniter</h1>
- <div class="row">
- <div class="col-sm-3">
- <h3 class="text-center">Add New Post</h3>
- <br>
- <form method="POST" action="<?php echo base_url(); ?>posts/addPost">
- <div class="form-group">
- <label>Post Title:</label>
- <input type="text" class="form-control" name="post_title" required>
- </div>
- <div class="form-group">
- <label>Post Text:</label>
- <textarea class="form-control" name="post_text" rows="5" required></textarea>
- </div>
- <button type="submit" class="btn btn-primary">Submit</button>
- </form>
- </div>
- <div class="col-sm-9">
- <table class="table table-bordered">
- <thead>
- <th>PostID</th>
- <th>Title</th>
- <th></th>
- </thead>
- <tbody>
- <?php
- foreach($posts as $post){
- echo "
- <tr>
- <td>".$post->id."</td>
- <td>".$post->title."</td>
- <td><a href='".base_url()."post/".$post->post_slug."' class='btn btn-info btn-sm'>View</a></td>
- </tr>
- ";
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>How to Create a SEO Friendly URL in CodeIgniter</title>
- <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <h1 class="page-header text-center">Create a SEO Friendly URL in CodeIgniter</h1>
- <div class="row">
- <div class="col-sm-8 col-sm-offset-2">
- <h5><b>Title:</b> <?php echo $post_row['title']; ?></h5>
- <p><b>Text:</b> <?php echo $post_row['post_text']; ?></p>
- </div>
- </div>
- </div>
- </body>
- </html>
Add new comment
- 1150 views