Codeigniter force all traffic to specific controller and method - php

I am sorry if this is a duplicate but after hours of searching the interwebs i am drawing up a a blank...
I am trying to remove force all traffic to a specific controller and method so my url will read one thing but be another. the end result would be something like this:
https://www.domain.com/home/pages/about
Would become this:
https://www.domain.com/about
I have tried using the routes to no avail. Do not get me wrong, I have the default set to use that route and the main page loads just fine but sub-pages do not follow it and give a 404 error.
Default route:
$route['default_controller'] = 'home/pages';
One of the many variations I have tried:
$route['home']['pages'] = 'home/pages';
Is this possible through the routes?

I am posting this because 30 seconds before I was about to submit this I read something on another page (the codeigniter manual) that I missed before...
The fix to this is to add a custom route like follows:
$route['default_controller'] = 'home/pages';
$route['(:any)'] = 'home/pages';
That along with the default route forces all pages to use the home controller and the pages method. It gives nice pretty URL's.

If you also want to override 404's, for example you are DB powered system with base slugs in the URL:
$route['default_controller'] = 'home/pages';
$route['404_override'] = 'home/pages';
$route['(:any)'] = 'home/pages';

Related

Setting Routing rules for different classes in codeigniter

My application URL "localhost/crdlabs/PHP" goes to "localhost/crdlabs/home/display/PHP" which is done using route.php. The rule is as follows.
$route['(:any)'] = "home/display/$1";
Now that I have another controller class called displayarticles(). The current URL for this class is "localhost/crdlabs/displayarticles/article/learning-coding". I understand that I cannot use the routing above for the new controller. How to set a routing rule for the current one to make the URL look like "localhost/crdlabs/learning-coding".
Note : learning-coding part is dynamically set which means there are several different articles that should go to the same controller.
Any help/advise please.
The current routing rule.
$route['(:any)'] = "home/display/$1";
Will routes anything appearing after localhost/crdlabs/argument to localhost/crdlabs/home/display/argument.So the route localhost/crdlabs/learning-coding will be redirected to localhost/crdlabs/home/display/learning-coding.So you can not use like this.
But I suggest you to show your articles
localhost/crdlabs/displayarticles/article/learning-coding
To
localhost/crdlabs/articles/learning-coding
using the following routing rule.
$route['articles/(:any)'] = "displayarticles/article/$1";
Will be best.

Codeigniter 3.1.0: Only default route is working

I will preface my question by saying I have searched high and low for a solution to my problem, including, but not limited to, Stackoverflow, YouTube and Google.
My issue: Only my default route is working in Codeigniter. My default route is set to a controller called Home which loads a home.php view. That works fine. I have another controller called Pages which as of now has only one method, Contact which points to view of the same name.
My routes.php file looks like this.
$route['default_controller'] = 'home/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// All custom routs go below this line.
$route['contact'] = 'pages/contact';
If I should change the default rout to 'pages/contact' my contact page shows.
My setup is as follows:
Windows 10
XAMMP 5.6.12
PHP 5.6.12
Any help would be greatly appreciated.
Default controller name should be like this $route['default_controller'] = 'home'; not like your $route['default_controller'] = 'home/home';
And then try it the url www.site.url/pages/contact is working or not if working then try
$route['contact'] = 'pages/contact';
Also you can debug your route with this link.

Purpose of routes.php in codeigniter

I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
Let me first take your through routing and then tell you about the meaning of those lines in routes.php.
You should start with URI Routing. In short, what you do with routes is that you map a certain URI with a controller/method/parameter statement.
Examples
See the following examples taken from the user guide:
So, something like example.com/journals can be routed to the blogs controller.
$route['journals'] = "blogs";
Another good example is when you are building a product catalogue and you need example.com/product/some_id to be routed to a controller catalog:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
In the example above, catalog will be the controller, product_lookup_by_id will be the method and $1 is the parameter which is picked up from the URI.
Answer to your question
You have asked:
I just want to know the purpose of this on routes.php
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";
The default_controller is quite obvious. This means welcome/index will be loading whenever `example.com/index is being requested.
scaffolding_trigger was deprecated in 1.7 but you can read about it. Scaffolding was a method which you could use to seed data in your database.
$route['default_controller'] = "welcome";
This is the default controller which codeigniter would start with when you didnt specify a controller to the URL.
url:
ip/monitor/index.php
This will trigger the default controller aka welcome.php
url:
ip/monitor/index.php/controller
This will, however, trigger your given controller and not the default one
This is mostly used to asign a index page as the start page.
I'm not sure about $route['scaffolding_trigger'] = ""; as i never used it. But according to the comments it has been removed in version 2.0
Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions.
For more detail please see this link :
http://ellislab.com/codeigniter%20/user-guide/general/routing.html

CodeIgniter __remap() routing more than one controller

I am trying to create a URL shortener using CodeIgniter 2.
I have 2 controllers: main and api.
For redirecting a short link through the router, I am using this setting in config/routes:
$route['(.*)'] = "main/$1";
along with a method in the main controller which should work. However, the controllers won't start. Please help me to solve this problem.
You controller "any" isnt called because it falls into that regex, so it's routed to main.
In order to exclude "any" from this rule you need to create a special rule for that, keeping in mind that for CI rules are cascading , so they're executed in the order they're presented
Note: Routes will run in the order
they are defined. Higher routes will
always take precedence over lower
ones.
So, you would have:
// reserved routes must come before custom routes
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['any'] = //your rule here. maybe "any". ?
$route['(.*)'] = "main/$1"; // CI also provides you with `(:any)` rule, that mateches any character.
More on this here: Uri routing

Codeigniter Routing not equal to pattern

I am working on a codeigniter app and am having some trouble wrapping my head around a routing issue. Basically I would like all routes to map to an specific controller action by default, but I would also like to able to specify an array of routes (or ideally initial url segments) which shouldn't follow this pattern.
By way of example:
If I enter domain.com/username it maps to domain.com/controller/method/show/username
If I enter domain.com/account it maps to domain.com/account
Any help very gratefully received!
James
Open config/routes.php and add the following:
$route['(:any)'] = "controller/method/show/$1";
Please see the link below for more routing concepts.
http://codeigniter.com/user_guide/general/routing.html
Routes will run in the order they are defined. So in your routes file, put the routes for other controllers you still want to work above your catch-all for usernames:
$route['default_controller'] = 'home'; //so root url still works
$route['accounts'] = "accounts";
$route['accounts/(:any)'] = "accounts/$1";
...
$route['(:any)'] = "controller/method/show/$1";

Categories