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

I'm moving a site over to CodeIgniter. It currently serves a LOT of urls which we have painstakingly tweaked for SEO. I definitely plan to try and organize my site's code by creating appropriate controllers. For example, http://example.com/help would route to a controller class named Help in the file help.php in the controllers folder.

I'm wondering, however, if it's possible to set up a controller that gets called when none of my existing controllers match. I.e., a 'catchall' or 'fallback' controller. Is this possible? I tried creating a _remap function on my default controller but it doesn't seem to catch this type of thing:
http://example.com/this-is-not-a-controller-name

I'm really hoping I don't have to specify every arbitrary possibility by defining some controller for it or making an entry in the routes.php file.
Reply
#2

I'm interested in this also. The OP wants a fallback controller, rather than a default controller and I think this is desirable. I'll track this thread for any suggestions.
CI 3.1 Kubuntu 19.04 Apache 5.x  Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply
#3

Check this thread, there is the _remap method as example http://forum.codeigniter.com/thread-511....ght=_remap

Reply
#4

(12-16-2014, 11:10 AM)Rufnex Wrote: Check this thread, there is the _remap method as example http://forum.codeigniter.com/thread-511....ght=_remap

You need to read my post more carefully, Rufnex. I'm quite familiar with the _remap function. The problem I am having is that I want to catch urls that do not correspond to any defined Controller:
sneakyimp Wrote:I tried creating a _remap function on my default controller but it doesn't seem to catch this type of thing:
http://example.com/this-is-not-a-controller-name
Reply
#5

Ok i had a quick reading depend on my time ^^. If you want caht urls that have no controller, you got an 404 error. In route.php you can route this to your wanted default controller

PHP Code:
$route['404_override'] = 'yourcontroller'

Is this what you want?

Reply
#6

(12-16-2014, 12:13 PM)Rufnex Wrote: Ok i had a quick reading depend on my time ^^. If you want caht urls that have no controller, you got an 404 error. In route.php you can route this to your wanted default controller


PHP Code:
$route['404_override'] = 'yourcontroller'

Is this what you want?

Aha! I think that may in fact be what I want. Before I simply give up, I want to have a chance to try and route the url anyway. I will read about this setting. If you have any tips about how to configure a controller to handle these 404 overrides, please do post them!
Reply
#7

I rewrote some sites in the past. Their URLs were similar to these: http://site.com/index.php?page_id=16 , http://site.com/index.php?product_id=48, ... A made the default controller to serve these old URLs, it does 301 redirections to the replacing URLs http://site.com/page/16 , http://site.com/product/48. Later I add slug support, so http://site.com/page/16 may be accessed as http://site.com/about-us for example.

It is important redirections to the new URLs to be done properly https://support.google.com/webmasters/answer/93633

The short version of your answer is - use the default controller. If there are no get parameters just display the home page, otherwise make server redirections to the new addresses.
Reply
#8

(12-17-2014, 12:47 PM)ivantcholakov Wrote: I rewrote some sites in the past. Their URLs were similar to these: http://site.com/index.php?page_id=16 , http://site.com/index.php?product_id=48, ... A made the default controller to serve these old URLs, it does 301 redirections to the replacing URLs http://site.com/page/16 , http://site.com/product/48. Later I add slug support, so http://site.com/page/16 may be accessed as http://site.com/about-us for example.

It is important redirections to the new URLs to be done properly https://support.google.com/webmasters/answer/93633

The short version of your answer is - use the default controller. If there are no get parameters just display the home page, otherwise make server redirections to the new addresses.

I appreciate your input, but our URLS are nothing like those that you describe in your post. An example url is:
http://example.com/salvage-product-aucti...tring-1234

In addition to those product-related urls, we have others for account actions, support, help, documentation, etc. The site logic is quite elaborate so the suggestion 'use the default controller' doesn't seem to work to me. How would one route that url to any particular controller?

Also, the idea of a single controller for our elaborate site seems like a very poor idea. It would be MASSIVE.
Reply
#9

(This post was last modified: 12-17-2014, 05:37 PM by ivantcholakov. Edit Reason: A typo. )

Then I would try something like this.

Code:
$route['(.+)'] = 'catchall/$1'; // Read the segments in Catchall controller.

But I don't have the time to test it. Just an idea.
Reply
#10

Sorry,

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




Theme © iAndrew 2016 - Forum software by © MyBB