Viewed   194 times

I have heard of people using slugs for generating clean urls. I have no idea how it works. Currently i have a codeigniter site which generates url's like this

www.site.com/index.php/blog/view/7

From what i understand by maintaining a slug field it is possible to achieve urls like

www.site.com/index.php/blog/view/once-upon-a-time

How is this done? Especially in reference to codeigniter?

 Answers

5

I just store the slugs in my database table, in a column called slug, then find a post with the slug, like this:

public function view($slug)
{
    $query = $this->db->get_where('posts', array('slug' => $slug), 1);

    // Fetch the post row, display the post view, etc...
}

Also, to easily derive a slug from your post title, just use url_title() of the URL helper:

// Use dashes to separate words;
// third param is true to change all letters to lowercase
$slug = url_title($title, 'dash', true);

A little bonus: you may wish to implement a unique key constraint to the slug column, that ensures that each post has a unique slug so it's not ambiguous which post CodeIgniter should look for. Of course, you should probably be giving your posts unique titles in the first place, but putting that in place enforces the rule and prevents your application from screwing up.

Thursday, October 20, 2022
4

I think the problem is that you've created an HTML form with GET method, which automatically opens the URL that way you specified as the result. If you want to submit your search query like the desired one, you should hack the form with some JavaScript to call your good-looking URL like this:

<form method="get" action="/search/" onsubmit="return false;">
<input type="search" name="q" value="querystring" />
<input type="submit" onclick="window.location.href=this.form.action + this.form.q.value;" />
</form>
Sunday, September 18, 2022
1

Say you needed to call a function called get_user_info that retrieves the users info from a database. You could have a function like this:

class Home extends Controller {

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

    function index() {
        $user = $this->get_user_info($_SESSION['user_id']);
        echo "Hello " . $user['first_name'];
    }

    function get_user_info($user_id) {
        $query = $this->db->query("SELECT * FROM users WHERE user_id = ?", array($user_id));
        return $query->row_array();
    }
}

However, what if you needed to call get_user_info on another page?

In this scenario you would have to copy and paste the function into every page making it difficult to maintain if you have lots of pages (what if you needed to change the query to JOIN onto another table?)

This also goes against the don't repeat yourself principle.

Models are meant to handle all data logic and representation, returning data already to be loaded into views. Most commonly they are used as a way of grouping database functions together making it easier for you to change them without modifying all of your controllers.

class User extends Model {

    function __construct() {
        parent::Model();
    }

    function get_user_info($user_id) {
        $query = $this->db->query("SELECT * FROM users WHERE user_id = ?", array($user_id));
        return $query->row_array();
    }

}

In the above we have now created a model called user. In our home controller we can now change the code to read:

class Home extends Controller {

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

    function index() {
        $this->load->model('user');
        $user = $this->user->get_user_info($_SESSION['user_id']);
        echo "Hello " . $user['first_name'];
    }
}

And now you can change your get_user_info function without having to change X number of controllers that rely on the same function.

Sunday, October 9, 2022
 
daiwei
 
3

Where does $slug come from?

Your URI looks like this: www.example.com/controller/method/arg1/arg2/arg3 (w/o query string)

Is there anything missing?

Well, there is few things you should do:

  1. Use autoload (config/autoload.php) to load your most used models, or if this model is not used widely at least load it at class constructor.

  2. You are not passing $slug argument to model method, this will not work even if you will fix your routing.

    $data['single_news'] = $this->news_model->get_single();

  3. Better to show 404 if you can't find a slug, don't refetch all data on fail.

To fix 404 error follow those steps:

  1. Check your .htaccess file for errors

  2. Check uri_protocol setting in config

  3. Try routing like this $route['news/((.+)$)?'] = "news/singe_news/$1"; (this should replace both routes for news)

Saturday, December 17, 2022
 
2

I found the way to do it. Redirecting does not keep the data to be shown. I used the code below to solve the problem:

if($this->form_validation->run() == FALSE)
{
    $this->index();
}
Wednesday, August 31, 2022
 
allesad
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :