Part of the EllisLab Network
   
1 of 3
1
$route[’:any’] issue
Posted: 16 June 2008 12:14 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

Hi,

I would like to pull pages from the database and give direct links:

www.mydomain.com/[page_name_in_db]

So I created the following rule in routes file:

$route[‘default_controller’] = “welcome”;
$route[’:any’] = “welcome/page_lookup”;

Now none of my controllers are being called, not even functions in my “welcome” controller. How can I fix that?

Thanks.

Profile
 
 
Posted: 16 June 2008 01:25 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Hi,
you’re trying to…

Looked at the source and what you’re trying should work.  There is something else a miss.

Need more info.  Put just the structure (not the entire thing) of your welcome class file in code tags here.  lets have a look.

Randy

 Signature 

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

Profile
 
 
Posted: 16 June 2008 01:59 PM   [ Ignore ]   [ # 2 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

EEssam,

OK.  Number of left_hand route arguments mustequal number of right_hand arguments ... so:

here is your fix:

$route[’:any/:any’]=“welcome/page_lookup”;

essentially will take a URL http://yourdomaindotcom/fred and invoke the page_lookup method of the welcome controller class.  Presumably you would then use $this->uri->segment(1) to retrieve the page named ‘fred’ from the table of your choice.

There you have it.

I had solved this a different way.  Thanks for you help.

Randy

 Signature 

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

Profile
 
 
Posted: 16 June 2008 03:11 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

Thanks Randy, but it didn’t work, I’m getting a not found error:

$route[‘default_controller’] = “welcome”;
$route[‘scaffolding_trigger’] = “scaffolding”;

$route[’:any/:any’] = “welcome/page_lookup”;

My Welcome controller:

<?php

class Welcome extends Controller {

  function Welcome()
  {
      parent::Controller();
     
      $this->load->scaffolding(‘category’); 
  }
 
  function index()
  {
      $this->load->view(‘welcome_message’);
  }
 
  function inde22x()
  {
      echo ‘g’;
  }
 
  function page_lookup()
  {
      echo ‘cool’;     
  }
 

}

Profile
 
 
Posted: 16 June 2008 03:13 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

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 03:17 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Do you see the welcome message?—it may be the ‘echo’ is ‘under’ (z-index layed) the other content.

 Signature 

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

Profile
 
 
Posted: 16 June 2008 03:42 PM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

I just got the not found page.

So, what is the correct way to accomplish the task then?

Profile
 
 
Posted: 16 June 2008 03:56 PM   [ Ignore ]   [ # 7 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Let’s start by commenting out the load->scaffolding call from the constructor for now.
I confirmed that only ONE :any wild card is required, so let’s remove the first :any and the slash, back to your original code:

[’:any’]

The php file named welcome_message.php does actually exist in your views directory, right?

 Signature 

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

Profile
 
 
Posted: 16 June 2008 04:08 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

Right.

Profile
 
 
Posted: 16 June 2008 05:00 PM   [ Ignore ]   [ # 9 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Hi EEssam,

um…are you getting the big fat ugly Object not Found error…or the pretty CI error surrounded by the red box?

 Signature 

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

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

What I’m eluding to is ... are you including the index.php or have you hacked your .htaccess file correctly to allow yourself to exclude it.  Just assumed earlier you had taken care of that.

Randy

 Signature 

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

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

I’m getting “the pretty CI error surrounded by the red box” smile

And I’ve index.php removed by a .htaccess file.

Profile
 
 
Posted: 16 June 2008 08:01 PM   [ Ignore ]   [ # 12 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

Using PHP5.2.5 and following with a completely clean build of CI it works flawlessly.  You have some fundamental problem that has you tweaked and I can’t figure…


.htaccess

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteRule
^(.+) index.php/$1 [L]

routes.php contains:

[’:any’]=“welcome/test_me”;

welcome

class Welcome extends Controller {
    
function Welcome()
    
{
        parent
::Controller();    
    
}
    
    
function index()
    
{
        $this
->load->view('welcome_message');
    
}
    
    
function test_me()
    
{
     
echo $this->uri->segment(1);
    
}
}


Randy

 Signature 

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

Profile
 
 
Posted: 16 June 2008 08:22 PM   [ Ignore ]   [ # 13 ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

Just create a new controller or a function in your welcome controller and you’ll not be able to reach it. You’ll get the function or controller name printed.

I think there should be somehow a first check if function or controller exists.

Profile
 
 
Posted: 16 June 2008 08:32 PM   [ Ignore ]   [ # 14 ]  
Lab Technician
RankRankRankRank
Total Posts:  1040
Joined  06-19-2007

OH!!  I’ve misunderstood along then.  Of course you cannot do that.  You’ve told CI explicitly to ignore all other requests for methods within the ‘Welcome’ class since you said “intercept calls to :any” method.  So what this does is “reroute” the call (in my example above) to the ‘test_me’ method and then echo’s the string if finds at the location of URI segment 1.  CI is doing exactly what you are asking it to do.

You must handle everything within your method call (test_me my example).

Does this make sense?

Randy

 Signature 

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

Profile
 
 
Posted: 16 June 2008 08:45 PM   [ Ignore ]   [ # 15 ]  
Lab Assistant
RankRank
Total Posts:  168
Joined  06-06-2008

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.

Profile
 
 
   
1 of 3
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120594 Total Logged-in Users: 47
Total Topics: 126631 Total Anonymous Users: 5
Total Replies: 665679 Total Guests: 476
Total Posts: 792310    
Members ( View Memberlist )