Part of the EllisLab Network
   
2 of 3
2
$route[’:any’] issue
Posted: 16 June 2008 09:49 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
RankRank
Total Posts:  146
Joined  01-02-2008

Umm, dont do this. Ever.
Spend the time and manually create your pages the way everyone else does.

Trying to cheat the system just ends up cheating you out.
-Matt

Profile
 
 
Posted: 16 June 2008 10:19 PM   [ Ignore ]   [ # 17 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007
EEssam - 17 June 2008 01:45 AM

It does. The question now how we can first check if there are controllers or methods with that name first ($this->uri->segment(1)), if so, we execute the controller or method normally otherwise test_me handle the job.

This goes back to my earlier post…

Wow,

I don’t really recommend anyone doing this…ever.  Just found out why this never crossed my mind.  This construct sort of throws all the flexibility of CI on the floor.

Would be better to come into the default controller and branch from there the way CI is designed to operate.

Just my two cents FWIW.

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 16 June 2008 10:20 PM   [ Ignore ]   [ # 18 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Essentially agreeing with XtraFile

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 16 June 2008 11:25 PM   [ Ignore ]   [ # 19 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  06-06-2008

You’re simply saying CI is not flexible enough for the task. :(

Profile
 
 
Posted: 16 June 2008 11:47 PM   [ Ignore ]   [ # 20 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

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 grin

Best Regards,

Randy

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 17 June 2008 11:29 AM   [ Ignore ]   [ # 21 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

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:

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

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

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 17 June 2008 11:40 AM   [ Ignore ]   [ # 22 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  3103
Joined  01-07-2008

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:

show_404($segments[0]); 

Change that to read (override the function, don’t edit the core file):

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.

 Signature 
Profile
MSG
 
 
Posted: 17 June 2008 12:12 PM   [ Ignore ]   [ # 23 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

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 wink

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 17 June 2008 01:04 PM   [ Ignore ]   [ # 24 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  3103
Joined  01-07-2008

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 .

 Signature 
Profile
MSG
 
 
Posted: 17 June 2008 05:29 PM   [ Ignore ]   [ # 25 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  06-06-2008

“override the function, don’t edit the core file” how can I do that? :S

Thanks.

Profile
 
 
Posted: 17 June 2008 05:38 PM   [ Ignore ]   [ # 26 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

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] )

 Signature 

My new therapist is working with me every day, the third one gave up… ohh

Profile
 
 
Posted: 17 June 2008 05:49 PM   [ Ignore ]   [ # 27 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  06-06-2008

So I should the whole _validate_request method?

Profile
 
 
Posted: 17 June 2008 06:02 PM   [ Ignore ]   [ # 28 ]  
Summer Student
Total Posts:  1
Joined  06-17-2008

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.

Profile
 
 
Posted: 17 June 2008 07:20 PM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  742
Joined  02-24-2008

If you are using .htaccess to rewrite a file, try setting your URI_PROTOCOL in config.php to REQUEST_URI.

That might work!

 Signature 

THE CODEIGNITER HANDBOOK - A new CI book for everybody!
—-
Freelance Web Developer - @jamierumbelow - http://jamieonsoftware.com

Profile
 
 
Posted: 17 June 2008 07:58 PM   [ Ignore ]   [ # 30 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  3103
Joined  01-07-2008

@eesam extending core classes.

In this case you will need to override the whole function since the 404 stops the script.  There are times when you just want to add something to the end, in which case you would user parent::function_name() to call the parent function.

Here’s how you would go about doing it for this example.  Create a file called MY_Router.php in the application/libraries folder:

class MY_Router extends CI_Router {
    
    
/**
     * Constructor
     *
     * @access    public
     */
    
function MY_Router()
    
{
        parent
::CI_Router();
    
}
    
    
// --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    
function _validate_request($segments)
    
{
        
/* Copy the original function in here and change that last line: */
        
        
return array('welcome''show_page'$segments[0]);
    
}

And since you’re not changing any core files this is pretty worry free and usually upgrades nicely.

Steve Wright’s method is easiest if you only have a few other pages, but it does get tedious with a lot of them.

 Signature 
Profile
MSG
 
 
   
2 of 3
2