1

In my CodeIgniter installation, my routes file is currently:

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

//routes
$route['about']              = 'page/view/about';
$route['(:any)']             = 'page/view/$1';
$route['default_controller'] = "page/view";

My question is - do I need to make a new $route call each time I have a new page, or is there a way to do it automatically? My page controller will be for my static pages...home, about, contact, faq, etc.

Do I need to specify each static page?

This might also stem to when I get to the registration part of the code. How will I automatically give a user their own route?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    Carefully read the [manual](http://ellislab.com/codeigniter/user-guide/general/routing.html), especially the "Examples" section. That said, CodeIgniter has one of the weakest routing systems among all the php frameworks that i have seen. – tereško Oct 17 '13 at 00:10
  • Not to be off-topic, but another thing I'm missing that "Twig" template engine has is template inheritance. Do you recommend an alternative framework? –  Oct 17 '13 at 00:12
  • There is library for .. emm .. cross-breeding CodeIgniter and Twig: http://edmundask.github.io/codeigniter-twiggy/ , but since I am not using either of those, I have not further insights. – tereško Oct 17 '13 at 00:14
  • OK, thank you. Still, however, if you recommend any frameworks better than this before I get myself deep into it, please throw them out there. :D –  Oct 17 '13 at 00:18
  • If you are just now starting with CodeIgniter, it might be good idea to pick something else instead. Currently the framework [is experiencing problems](http://ellislab.com/blog/entry/ellislab-seeking-new-owner-for-codeigniter) of the organizational variety and might even soon be discontinued. Currently the best option in PHP is considered Symfony2 (but that's like pointing out the smartest kid in remedial class). Also, I hope that you are not looking at frameworks to "learn OOP" or "learn MVC", because to use them you already should have solid grasp on OOP and none of them actually applies MVC – tereško Oct 17 '13 at 00:46
  • I already know (some) OOP, however I'm new to MVC. Thanks for your recommendation, however I stumbled on "Fuel" and am looking into that. –  Oct 17 '13 at 01:57
  • @tereško if you want to add an answer, I will accept it. –  Oct 17 '13 at 02:04
  • 1
    If "answer" can fit in a comment, then it is not worth posting. As for MVC, I would recommend for you to try going through [this list](http://stackoverflow.com/a/16356866/727208) of materials. Actually the first MCV-related link there is "GUI Architectures", but you need to understand the stuff above to have any hope in comprehending it. – tereško Oct 17 '13 at 02:11

1 Answers1

0

One way to avoid manually setting routes for each page is to create a Page controller and route all uri's to this controller.

Routes.php:

// Default controller
$route['default_controller'] = "page/index";

// Page controller catch all
$route['(:any)'] = 'page/view/$1';

The order of your routes.php file is important, and these should be the last two lines in the file. If you have other controllers (i.e. News/Blog/Products/Whatever) their routes should go above these two routes.

Page.php

class Page extends CI_Controller {

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

    public function index()
    {
         // This will be where your load your top page (homepage)
    }

    public function view($uri)
    {       
         // This will be where you load all other content pages (about/info/contact/etc)
         echo $uri;
    }   
}

Obviously this is very basic, but it gives you an idea of how to achieve automatic routing for Pages. Once you know the uri, you can use that to pull information about that page from a csv/database/textfile and then load the appropriate view for that uri.

Jeemusu
  • 10,415
  • 3
  • 42
  • 64