Welcome Guest, Not a member yet? Register   Sign In
Friendly URLs/Slugs for content
#1

[eluser]doubleplusgood[/eluser]
Hi there,

I'm trying to implement friendly URLs for a 'Testimonials' section of my website. I *think* I am almost there with it, except that when I try to view a testimonial I am presented with a 404 error.

This is my testimonials Controller;
Code:
class Testimonials extends Controller {

    function Testimonials()
    {
        parent::Controller();
        
        $this->load->model('testimonials_model');
    }
    
    function index()
    {
        $view_data = array();
        
        $view_data['view_file'] = 'testimonials/index';
        
        $view_data['testimonials'] = $this->testimonials_model->get_many_testimonials();
        
        $this->load->view('layout', $view_data);
    }

    function testimonial()
    {
        $slug = $this->uri->segment(3);
        
        if ( $slug === FALSE )
        {
            redirect('testimonials');
        }
        
        $view_data = array();
        $view_data['testimonial'] = $this->testimonials_model->get_testimonial($slug);
        
        if ( $view_data['testimonial'] === FALSE )
        {
            redirect('testimonials');
        }
        
        $view_data['view_file'] = 'testimonials/view';
        
        $this->load->view('layout', $view_data);
    }

}

And this is my Model;
Code:
class Testimonials_model extends Model {
        
        function Testimonials_model()
        {
            parent::Model();
        }
        
        function get_many_testimonials($num = 10)
        {
            $this->db->orderby('title', 'asc');
            $query = $this->db->get('mgb_testimonials', $num);
            
            if ( $query->num_rows() > 0 )
            {
                return $query->result();
            }
            
            return array();
        }
        
        function get_testimonial($slug)
        {
            $this->db->where('slug', $slug);
            $query = $this->db->get('mgb_testimonials', 1);
            
            if ( $query->num_rows() == 1 )
            {
                return $query->row();
            }
            
            return FALSE;
        }

    }

My views/testimonials/index.php contains the following code to access an individual testimonial;
Code:
<?php foreach($testimonials as $testimonial):?>
<a href="<?=site_url('testimonials/view/'.$testimonial->slug)?>"><?=$testimonial->title?></a>
<?php endforeach;?>

But, I get the 404 error trying to access one of the testimonials at;
http://localhost:8888/ci/mgbuilders/test...number-one

I was wondering if anyone might be able to spot any schoolboy errors? Big Grin Incidentally, I am storing the slug as a separate column for each testimonial in the database.

Thank you.
#2

[eluser]bretticus[/eluser]
Well, unless you have an .htaccess files hiding your index.php page, your URL should probably be:

http://localhost:8888/ci/mgbuilders/inde...number-one

Not sure why you are trying to surf directly to your views either. Perhaps, take another look at the manual.
#3

[eluser]doubleplusgood[/eluser]
@bretticus
I am indeed hiding the index.php. I would also like to hide the 'testimonial' segment of the URL so that users will view each testimonial at something like; http://localhost:8888/ci/mgbuilders/test...number-one
#4

[eluser]mattpointblank[/eluser]
Shouldn't http://localhost:8888/ci/mgbuilders/test...number-one be http://localhost:8888/ci/mgbuilders/test...number-one instead?
#5

[eluser]doubleplusgood[/eluser]
@mattpointblank
Thanks man, I think you could be right. Big Grin I guess I shouldn't be working at 2am. Lol




Theme © iAndrew 2016 - Forum software by © MyBB