Welcome Guest, Not a member yet? Register   Sign In
$route[':any'] issue
#21

[eluser]Randy Casburn[/eluser]
That's one way to look at it. I suppose the other way to look at it is that CI is very flexible in it's approach, but you're not flexible in your design.

Either way one of you has to budge a little.

You can make CI do what you want, you just have to open you mind a little to make it bend to your will. You'll then see how really flexible it is :-)

Best Regards,

Randy
#22

[eluser]Randy Casburn[/eluser]
Hi EEssam,

Just for fun, I thought I would put something together to demonstrate how to do this. I'm certain there are 10 ways, but this is one. The design concept is simply to have a somewhat static facing web that would be fantastic for SEO purposes. The true application that resides behind this static site, built with CI, of-course, would never be built this way but would be discovered because of the great SEO attributes the single-word-from-root web page name provides.

Disclaimer: Put some error checking in here somewhere!

Controller:
Code:
class Home extends Controller
{
    var $pageData;
    var $uriSegments;
    
    function Home()
    {
        parent::Controller();
        $this->uriSegments = $this->uri->total_segments();
    }

    function index()
    {
        $this->pageTop();
        $this->load->view('content/public/home');
        $this->pageBottom();
    }
  
   /* Routes.php is used to reroute [:any] method here. Any tiered levels off
    * the root of your web must be accounted for in this structure. In this
    * example, there is the 'home' page, a set of 'articles', and a 'portal'.
    * Note the left menu will only be seen on the 'home' page and the default
    * pages.  The left menu will not be seen on the 'articles' pages or the
    * portal page.
    */
   function load_page(){
        /* I Use ajax for all page updates. So each time a link is click
         * that would return to the home page, the index() method is bypassed
         * due to the routing.  So this must be here to accommodate both the
         * ajax requests and the rerouted URIs.
         */
        if ( $this->uri->segment($this->uriSegments) == 'home' ){
            $view_to_show = 'home';
            $this->load->view('content/public/left_menu');
        }
        elseif ( strstr($this->uri->uri_string(),'articles') ) {
            $view_to_show = 'articles';
        }
        elseif ( strstr($this->uri->uri_string(),'portal') ) {
            $view_to_show = 'eyeOSlogon';
        }
        else {
            $view_to_show = 'content';
            $this->load->view('content/public/left_menu');
        }

        if($this->uri->segment($this->uriSegments) == 'articles') $this->getPageData('strategy');
        else $this->getPageData($this->uri->segment($this->uriSegments));
        $this->load->view('content/public/'.$view_to_show, $this->pageData);
   }

   function getPageData($page){
      $this->load->model('Getmystuff');
      $this->pageData = $this->Getmystff->getPageDataFromDB($page);
   }

Model function from file Getmystuff.php
Code:
function getPageDataFromDB($page)
    {
    /* presumes a DB with a table named content
     * with at least a row named 'alias' that has
     * the lower case page name
     */
        $query = $this->db->getWhere('content','alias="'.$page.'"');
        $aPageMeta = $query->row_array();
        return $aPageMeta;
    }

For the views: Note I'm passing $this->pageData array. The pageData array is simply constructed from the DB rows returned by the model. Use the variable representations of those rows within your views to show the content in your view templates.

I hope this more thoroughly answers your question. Most importantly, shows you how awsome CI can really be once you understand it's flexibility.

So here is one 'realization' of use CI and construct your URLs with the page name right after the FQDN thus:
http://mydomainname.com/mypage or http://mydomainname.com/mypage.html

Regards,

Randy
#23

[eluser]Pascal Kriete[/eluser]
It can be done - without routes.

In router.php, the function _validate_request checks if the controller exists. Now at the very bottom of that function you have this line:
Code:
show_404($segments[0]);

Change that to read (override the function, don't edit the core file):
Code:
return array('welcome', 'show_page', $segments[0]);

So if the controller doesn't exist, it will call the welcome controller show_page function and pass the name of the page in as an argument.

Make sure to throw a 404 if the page doesn't exist though.
#24

[eluser]Randy Casburn[/eluser]
When I looked at router.php and thought about overriding it, the first thing I thought of was how I personally have a problem with loosing track of mods that change the nature of the config files. I've tried to stop overriding the core because I keep forgetting what I have rendered irrelevant.

In this case, I'm almost certain we would render the 'default_controller' config setting moot. Later on (months, weeks, years, etc.), I would likely be the fool trying to figure out why that setting just wasn't working ;-)
#25

[eluser]Pascal Kriete[/eluser]
At the top of my core extensions I try to list the changes I make. Also, this does not override the default_controller setting (replacing a 404 statement, remember). I do use this occasionally to catch silly misspellings and redirect them to the homepage. Misspelled functions are another thing, but most people don't type that much Smile .
#26

[eluser]EEssam[/eluser]
"override the function, don’t edit the core file" how can I do that? :S

Thanks.
#27

[eluser]Randy Casburn[/eluser]
When you create a class that extends another class (a super class) you override the the methods of the super class by naming your methods with the same name of the super class' methods. If you want to retain the functionality of the super class' method, you'll need to duplicate that functionality in your method because the super class' method will no longer be available to you.

Make sense?

(edited to clean up the terminology a little [method vs. function] )
#28

[eluser]EEssam[/eluser]
So I should the whole _validate_request method?
#29

[eluser]Steve Wright[/eluser]
I am doing a similar thing in one of my applications using...

$route['(:any)'] = "display/index/$1";

The index function of the display controller accepts one argument which is the page name in the database. If the query returns no matching page, it serves a 404.

The trick is that routes to any other controllers/functions must be listed BEFORE the catch-all route shown above.
#30

[eluser]Jamie Rumbelow[/eluser]
If you are using .htaccess to rewrite a file, try setting your URI_PROTOCOL in config.php to REQUEST_URI.

That might work!




Theme © iAndrew 2016 - Forum software by © MyBB