Welcome Guest, Not a member yet? Register   Sign In
F1, F1, F1 please! Routing / remapping
#1

[eluser]Cippo[/eluser]
Hello everyone,

I use CodeIgniter and Doctrine for a custom CMS that I build for my website.

Right now, my uri is something like: http://example.com/home/index/1 ( for the homepage ) and http://example.com/home/index/2 ( for about page - ps: there will be several pages for main navigation ).

I want to ask you if there is a way to access http://example.com/about instead of example.com/home/index/2. In this moment, I have two tables in my database: content and pages and I set up a relationship between them.

Here’s the function for home model:
Code:
class Home_Model extends Doctrine_Record
{

    public function getContentsArray()
    {
        $conts = Doctrine_Query::create()
            ->select('c.body, c.type, c.title, c.page_id')
            ->addSelect('p.id, p.name, p.title')
            ->from('Home_Model c, c.Pages p')
            ->where('p.id = ?', $this->id)
            ->andWhere('p.id = c.id')
            ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
            ->execute();
            
            
        return $conts;
    }

    public function getPagesListArray()
    {
        $pages_list = Doctrine_Query::create()
            ->select('p.name, p.title')
            ->from('Pages_Model p')
            ->setHydrationMode(Doctrine::HYDRATE_ARRAY)
            ->execute();
            
        return $pages_list;
    }
    
    public function setTableDefinition()
    {
        $this->hasColumn('title', 'string', 255);
        $this->hasColumn('body','string', 65535);
        $this->hasColumn('type', 'integer');
        $this->hasColumn('page_id', 'integer');
    }
    
    public function setUp()
    {
        $this->setTableName('content');
        $this->hasOne('Pages_Model as Pages', array(
            'local' => 'page_id',
            'foreign' => 'id'
        ));
    }
}

And my controller:
Code:
public function index($id)
    {
        $home_model = Doctrine::getTable('Home_Model')->find($id);
        
        $vars['conts'] = $home_model->getContentsArray();
        $vars['page_id'] = $home_model['page_id'];
        $vars['title'] = $home_model['Pages']['title'];
        $vars['page_name'] = $home_model['Pages']['name'];
        $name = $home_model['Pages']['name'];
        $vars['content_view'] = 'home';
        // $vars['name'] = $home_model['name'];
        $vars['pages_list'] = $home_model->getPagesListArray();
        $this->load->view('home', $vars);
    }
I don’t want to set each page in routes.php like
Code:
$route[‘about-me’] = “home/index/2”;
. I found a route that looks like
Code:
$route['(:num)'] = 'home/index/$1';
which is a little more convenient and I can access the pages using: http://example.com/4

BUT what I want is to replace the id with the 'slug' that I have created in my database Pages table.

PS: I've been looking over the forum all day long but couldn't find that something. I've got some headaches so I'm asking for some help. Thanks
#2

[eluser]danmontgomery[/eluser]
You can define a catch-all route:

Code:
$route['(:any)'] = 'home/index/$1';

Then do some pseudo-routing in the controller method.

Code:
public function index($slug) {
  $query = $this->db->where('slug', $slug)->get('table_name');
  if($query && $query->num_rows() == 1) {
    $row = $query->row();

    do_some_function($row->id);
  } else {
    show_404();
  }
}
#3

[eluser]Cippo[/eluser]
Shall I add the $slug to the index method? I don't really get it. I already have the index file in my controller.
#4

[eluser]danmontgomery[/eluser]
Yes... Each segment in the url after the method name is a parameter passed to that method. So, with that route, something like:

Quote:http://www.yourdomain.com/find-this-page

Would pass the value "find-this-page" to the index function.




Theme © iAndrew 2016 - Forum software by © MyBB