Welcome Guest, Not a member yet? Register   Sign In
Is it possible to establish a catchall controller method?
#11

We have a walkthrough to use in this case

$route['(\S*)\.html?'] = "pagecreator/generate/$1";

this will let all the url with end is .html or .htm route to the controller and function that you define. cheers!!
Follow us at https://twitter.com/LIBRETeamStudio to get a lot of new cool stuff
Reply
#12

(12-17-2014, 05:45 PM)ivantcholakov Wrote: Sorry,


Code:
$route['(.+)'] = 'catchall/index/$1';

Wouldn't this router mean that one's entire site was always handled by that one router? This is not what I want. I want CodeIginiter to first match any controllers I have defined in the controllers directly and only after none have been matched do I want to rely on the catchall. I believe the 404_override is what I want here.
Reply
#13

(12-19-2014, 10:29 AM)sneakyimp Wrote:
(12-17-2014, 05:45 PM)ivantcholakov Wrote: Sorry,



Code:
$route['(.+)'] = 'catchall/index/$1';

Wouldn't this router mean that one's entire site was always handled by that one router? This is not what I want. I want CodeIginiter to first match any controllers I have defined in the controllers directly and only after none have been matched do I want to rely on the catchall. I believe the 404_override is what I want here.

No. The routes file is read from top to bottom so if you put this at the end of your routes file it should only catch it if you don't have any other routes setup. That being said, you need to have other routes setup.
Reply
#14

(12-19-2014, 12:13 PM)albertleao Wrote:
(12-19-2014, 10:29 AM)sneakyimp Wrote:
(12-17-2014, 05:45 PM)ivantcholakov Wrote: Sorry,




Code:
$route['(.+)'] = 'catchall/index/$1';

Wouldn't this router mean that one's entire site was always handled by that one router? This is not what I want. I want CodeIginiter to first match any controllers I have defined in the controllers directly and only after none have been matched do I want to rely on the catchall. I believe the 404_override is what I want here.

No. The routes file is read from top to bottom so if you put this at the end of your routes file it should only catch it if you don't have any other routes setup. That being said, you need to have other routes setup.

Thanks for the clarification. This approach would mean that I must specify some routing rule for each of my controllers rather than just letting CI do it's natural routing. This is undesirable because it means the devs on my project must all make changes to routes.php file if controllers are added or removed. I think he 404_override is preferable for this reason. Unless I'm missing something...
Reply
#15

"I think the 404_override is preferable for this reason. Unless I'm missing something..."
It might work, I don't know, but you probably will need to prevent 404 http return code on your ordinary pages instead of 200. And also the case of real 404 error would need to be detected. Sounds complicated to me.
Reply
#16

(12-21-2014, 05:47 PM)ivantcholakov Wrote: "I think the 404_override is preferable for this reason. Unless I'm missing something..."
It might work, I don't know, but you probably will need to prevent 404 http return code on your ordinary pages instead of 200. And also the case of real 404 error would need to be detected. Sounds complicated to me.

The http code is 200 when it hits your 404_override controller. I expect I'll have some pretty particular logic which can reliably distinguish a valid request from a bogus one. It may be fairly complicated, but the our search is complicated. My original reason for wanting this fallback controller is to parse the request and implement some previously existing search logic without the restrictions of a mod_rewrite file or routes.php or the default CI controller/method routing. PHP will give me a lot more flexibility.
Reply
#17

(This post was last modified: 12-26-2014, 04:05 PM by rfulcher.)

Would this method of AOP and point cuts help what you are trying to do?

http://www.sitepoint.com/explore-aspect-...igniter-1/


I didn't read through all of the posts but it seems like it could be accomplished with this method.
Reply
#18

(12-26-2014, 04:05 PM)rfulcher Wrote: Would this method of AOP and point cuts help what you are trying to do?

http://www.sitepoint.com/explore-aspect-...igniter-1/


I didn't read through all of the posts but it seems like it could be accomplished with this method.

I appreciate your input, but tend to think that the application of aspect-oriented programming would come later. Your post seems better suited to this other question that I have about creating base controller classes to enforce authentication.

As for the applicability of AOP in this context, it seems to me that my desire to have a catchall router is a consideration that precedes any AOP considerations -- at least as they are described in the article you linked. Or perhaps my routing decisions (e.g., the desire for a catchall router) are ones that affect AOP considerations? Seems to me that either
a) routing decisions must happen before AOP considerations come into play
or
b) effective routing of a request to its corresponding URL is a 'cross cutting concern' and we need to somehow apply this AOP concept to either my CodeIgniter configuration OR to the very structure of CodeIgniter itself.

Ultimately, I think the vocabulary utilized in the AOP discussion is pretty poorly chosen (e.g., Joinpoint, Pointcut, Advice, etc.) which makes the discussion that much harder.
Reply
#19

I use a route ... $route['404_override'] = 'oops';

This only kicks in if a controller has not been found conventionally. I have no other routing other than the welcome page.

And my Oops controller keeps a map of old vs new URLs and controllers, and tries to match the incoming request appropriately. If a match cannot be made, then it redirects to the site's search page.

The essence of my handler:

Code:
class Oops extends Application {

   // mapping of old to new controllers
   var $remap = array(
       'american' => 'travel',
       'artscrafts' => 'arts',
       ...
   );

   // mapping of old to new URLs
   var $redirect = array(
       'universalbooks' => '/shopping/promo/135',
       ...
   );
   
   function index() {
       $this->load->library('uri');
       $this->load->helper('url');

       // what are we working with?
       $segments = $this->uri->segment_array();
       $controller = $this->uri->segment(1);
       
       // do we have a straight redirect?
       if (isset($this->redirect[$controller])) {
           redirect($this->redirect[$controller]);
       }

       // do we have an outdated URL
       if (isset($this->remap[$controller])) {
           $segments[1] = $this->remap[$controller];
           $this->goaway($segments);
       }

       // did someone ask for a .php page?
       if (substr($controller, -4) == '.php') {
           $segments[1] = substr($controller, 0, -4);
           $this->goaway($segments);
       }

       // ok - give up ... go through the search
       redirect('/search/find/' . $this->uri->uri_string());

   }

   // redirect to newly constructed target
   function goaway($segments) {
       $target = "";
       foreach ($segments as $segment)
           $target .= "/" . $segment;
       redirect($target);
   }

}
James Parry
Project Lead
Reply
#20

Thanks for this, James. It looks a lot like what I'm after.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB