CodeIgniter show accessed route/translated controller on 404 page - php

Now, when CodeIgniter does not find a controller for a route or a route for the uri, it shows a generic 404 page. How can I show the accessed route or controller or something useful that would allow me to debug why it's showed a 404?
Code:
switch ($_SERVER['HTTP_HOST']) {
case 'multiplelines.domain.ro':
$route['default_controller'] = "subdomain"; break;
default: $route['default_controller'] = "welcome"; break;
}
$route['scaffolding_trigger'] = "";
$route['404_override'] = '';
if($_SERVER['HTTP_HOST'] == "www.domain.ro"
|| $_SERVER['HTTP_HOST'] == "domain.ro")
{
// URI like '/en/about' -> use controller 'about'
$route['^(romana|english|magyar)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(romana|english|magyar)$'] = $route['default_controller'];
}
else
{
}
Desired effect:
When on www or empty subdomain routing will occur like this:
/language/test will go to test controller
When on another subdomain (I just listed one case) will be routing to the subdomain controller as I just want to use one controller for those.
Note: Now when on www or empty subdomain there is code to choose the language automatically for /test route for test controller (without language directory in uri).

When CI dont find correct Route for a page it knows that there isn't such page so it is giving 404 NOT EXISTING..
If user try to open /sgasgasggsasga page at your site it gets 404 for the same reason..
You have to find by yourself why its showing 404...
Look at http://codeigniter.com/user_guide/general/routing.html for help :)

Enable error logging on your application.
Then, once you look up the errors, you will see the URI that was requested that generated the 404 error.

Will fill in answer properly when on computer, but have a look at Debugging routes in codeigniter? it has route logging

How can I show the accessed route or controller or something useful that would allow me to debug why it's showed a 404?
You could echo $_SERVER['REQUEST_URI'] in the 404 view to determine where the user was trying to go.

Related

Redirect from default controller to HMVC modules controller in codeigniter

How to redirect from CI default controller to HMVC modules controller?
I have a fresh installation of CI, in which I have defined default controller in /application/config/routes.php
$route['default_controller'] = 'welcome';
Now I am trying to use complete modular way and write everything in modules directory. For that purpose I have created a home app in
/application/modules/home/controllers/Home.php
Now the issue I am having is, I am not able to directly redirect to my home page using localhost/ci
This url is redirecting to default welcome.php
When I pass localhost/ci/index.php/home I can navigate to home page.
I tried using redirect, it didn't work.
I want to redirect to home page using localhost/ci url. Please help.
Change your default controller like this..
$route['default_controller'] = 'home';
if you want to redirect to particular method of your controller:Then
$route['default_controller'] = 'home/method_name';
just change your $route['default_controller'] = 'welcome';
to
$route['default_controller'] = 'folder_name/controller_name';
in your routes file.
and try

redirect with route on codeigniter

I am trying to routing the url to some Controllers that are in different folder than the url show... I think it is better an example, I have something like this:
$route['admin'] = 'admin/admin';
$route['admin/register'] = 'admin/login/register';
$route['admin/newuser'] = 'admin/login/newuser';
$route['admin/logout'] = 'admin/login/logout';
$route['admin/login'] = 'admin/login';
$route[''] = 'admin/admin';
All of them are working, but last. What I want to do is that someone type www.mydomain.com the system use the controller admin (placed on the folder admin), but I am getting a 404 always.
A fast, but not elegant solution I am thinking (but I wouldn´t want to do) is modifying the 404 file and test if there is no segment, and then redirect.
Any other idea?? Thank you.
You must set default controller for default page when user click you page
$route['default_controller'] = "admin";
it's set your default view for homepage
Step 1: Remove $route[''] = 'admin/admin';
Step 2:
In order to redirect to admin/admin when the user type URL www.mydomain.com, then you should do like this:
$route['default_controller'] = 'admin/admin';
Then it will redirect to admin controller and admin method if no controller after www.mydomain.com.
Have nice routing!
In config/routes.php
$route['default_controller'] = 'admin';
$route['404_override'] = 'admin';
When 404 occur it will load method which you provide in $route['404_override'].

custom URL mappings in codeigniter like redmine?

So redmine has a very peculiar url mapping style that i observed :
http://demo.redmine.org/projects/<project-name>/controller/action
samples :
http://demo.redmine.org/projects/produto/activity
http://demo.redmine.org/projects/produto/issues/new
http://demo.redmine.org/projects/produto/issues/gantt
http://demo.redmine.org/projects/produto/files
and the url changes as the project changes.
how do i do this in codeigniter ? I'm thinking it can be done with routes.php but so far i'm not able to get anywhere.
Looking for any help. Thanks.
Use the following function inside your "application/controllers/projects.php" controller:
public function _remap($method)
{
if ($method == 'project-name')
{
//display project1
}
elseif($method == 'project-name2')
{
//display project2
}
}
You can do the same for varying methods by extracting them from database
take a look here:
http://codeigniter.com/user_guide/general/controllers.html#remapping
you can also route your controller by using custom routes in application/config/routes.php
$route['example'] = "controller/function";
$route['example2/(:any)'] = "controller/function";
You use the routes file in application/config/routes.php
You would use something like this:
// the $1 maps to :any
$route['projects/produto/:any'] = "$1";
// the $1 maps to the first any, $2 maps to the second :any
$route['projects/produto/:any/:any'] = "$1/$2";
You will want mod_rewrite enabled if you are handling clean URL's. Otherwise expect the index.php/controller/action. I cant test it myself there, but you should refer to:
Once you add a route (It has to be called $route[] inside the configuration), refresh the page and try to go to the URL!
http://codeigniter.com/user_guide/general/routing.html
add this to your routes.php (btw: you need url-rewriting enabled for routes to work, ie. using .htaccess)
$route['projects/(:any)/(:any)/(:any)'] = "$2/$3/$1";
for example /projects/produto/issues/new will call the function new in the class issues and pass it the parameter 'produto'
also check http://codeigniter.com/user_guide/general/routing.html

How to set controller/action to Page Not Found

I have a controller and action which I'm accessing through a custom URL. The original route is still accessible though at the default location
zend.com/controller/action
How can I change this to simulate a "Page not found" when the user tries to access this URL? Is it possible?
If the action handler is used to respond to both URLs, you would first have to detect which URL is being requested (using $this->_request->getRequestUri()). If the default URL is detected I think the easiest way to create a "page not found" would be to use
$this->_redirect("/path/to/simulated/404/page")
and set up a controller and action to respond.
This won't actually send an HTTP 404, though. To do that, I think you would have to raise an exception within your action handler. I don't know what the official "zendy" way of doing this is, but this seems to work:
throw new Zend_Controller_Action_Exception('Not Found', 404);
You could change the main controller script to redirect a certain controller name and action name to a new page. But it's probably easier to add a new rule to the .htaccess file, indicating that this specific URL should be redirected to an error page. Example:
RewriteRule ^controller/action/?$ / [R=404,L]
Or redirect the page to an error page within your site:
RewriteRule ^controller/action/?$ /error/page-not-found/ [L]
You need to use:
$this->getResponse()->setHttpResponseCode(404);
And build your own 404 view
$this->view->message = 'Page not found';
Or you could forward to an error controller for example
$this->_forward('page-not-found', 'error');
Finally, if you have in your error controller
//...
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
//...
You can just do as #bogeymin said:
throw new Zend_Controller_Action_Exception('Not Found', 404);
If you are looking other solutions than mod_rewrite based, you may create a Regex Route to match the actions you need to hide.
The other solution is to restrict access to Actions using Zend_Acl, treating each action as an ACL resource.
But the simplest and most lightweight solution is still mod_rewrite in .htaccess.
Edit:
As you can see, this may be done in numerous ways. But probably, you will need some kind of the switch, to still allow somehow to access the "hidden" action. In this case, use:
mod_rewrite for quick implementation (switching requires the person to know the .htaccess rules)
Zend_Router - the person who knows the right route can still access the feature
Zend_Acl + Zend_Auth for scalable and secure solution.
If you don't need to have authenticated users, Zend_Acl combined with Zend_Router might be the solution.
For smart handling the exceptions and building ACL's, see this (and other posts on this blog):
Handling errors in Zend Framework | CodeUtopia - The blog of Jani Hartikainen
By default the router includes default routes for :module/:controller/:action/ and :controller/:action/. You can disable these with:
$router->removeDefaultRoutes();
then only routes you setup will work. If you still want to use the default routes for some other things, you'll either have to go with one of the other answers posted or add your own 'default' routes which will match all but the modules/controllers you have custom routes for.
If you don't want to remove the default route as #Tim Fountain suggests, you should do something like this in your controller (either preDispatch or whateverAction methods)
$router = $this->getFrontController()->getRouter();
$route = $router->getCurrentRouteName();
// if we reached this controller/action from the default route, 404
if ($route == 'default')
{
throw new Zend_Controller_Action_Exception('Not Found', 404);
}
I think, all answers above are incorrect. Those show ways to achieve the same thing, but present logic at the wrong place in your application, which eventually can cause trouble later on.
The correct part of your route logic is, how extremely simple, in the routes. What is missing is that the default route Zend_Controller_Router_Route_Module does not allow you to add exceptions to specific routes. So what you need to do, is remove the default route from your routes, and add a new custom route (which should function exactly as the default route, but allows excludes) at it's place.
You can write the new route by extending the class of the default route.
class My_Custom_Route extends Zend_Controller_Router_Route_Module
{
protected $_excludes = array();
public function exclude($abc)
{
//add to $_excludes here the controller/action you want to exclude
}
public function match($abc)
{
//add functionality here that denies if the mod/contr/action is in $_excludes
//you can also add this in a separate method
//re-use parent code
}
}
You can now add add the excludes for example in a config file, and load + add the excludes at the place you initiate the new Route (and add it to the router). Off you go.

Default Controller in CodeIgniter

I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".

Categories