0

As you know, CodeIgniter uses default URL routing of class/function/parameters. The class is a controller, and the function to be invoked inside the controller class is the second part of the URL. However, the user can easily change the URL to anything he wants, and I want my application to be able to display a graceful error message if the user is trying to view a function that does not exist. I looked through the CodeIgniter documentation on controllers and it doesn't seem to be listed there, or maybe I missed it. Here is an example of what I am talking about.

<?php

class Users extends MY_Controller {

    function index(){ //do something }

    function something_else(){ //do something else }

}

My application is written cleanly and smartly so there will not ever be a link inside my application that leads to anything except these two functions. However, the user can easily change the URL to anything he wants, such as http://www.mysite.com/welcome/another_thing. I do not have function another_thing, so if the user changed the URL to something like that, PHP currently yields the following error message.

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Users::$session

Filename: core/MY_Controller.php

Line Number: 15

Fatal error: Call to a member function userdata() on a non-object in /home/diftx/public_html/dol/application/core/MY_Controller.php on line 15

I currently have it extending MY_Controller instead of CI_Controller which I'm not sure if has anything to do with it. I would think that the application should redirect to my default view for 404 errors which is not_found.php however this is not happening.

tereško
  • 58,060
  • 25
  • 98
  • 150
skcin7
  • 783
  • 1
  • 10
  • 24
  • What is your purpose of not using codeigniters default controller? My default setup of codeigniter makes me go to a 404 page – Henesnarfel Feb 07 '12 at 19:45
  • The way my login system works uses some additional functions and variables in that controller and I couldn't think of anywhere better to put them. I added `parent::__construct()` in the constructor of my new controller thinking it would have all the same functionality of the default controller, but apparently not. – skcin7 Feb 07 '12 at 20:26
  • possible duplicate of [Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model](http://stackoverflow.com/questions/312511/codeigniter-i-am-looking-to-use-connect-to-a-different-database-for-one-of-my) .. also: http://stackoverflow.com/a/12769983/727208 – tereško Feb 12 '13 at 21:52

4 Answers4

1

You can use Remapping Function

Ivan
  • 491
  • 6
  • 15
0

You can use routes.php:

application/config/routes.php

$route['404_override'] = 'controller/error_function';
mirza
  • 5,685
  • 10
  • 43
  • 73
  • I have `$route['404_override'] = 'welcome/not_found'` defined in my routes.php page. However, it doesn't seem to be resolving to that desired page but I'm not sure why. I do have that 404 page set up as a view and stored in views/welcome/not_found.php. – skcin7 Feb 07 '12 at 20:06
0

From what it looks like from the error is that you've added session functionality to your customized controller and are referencing it incorrectly. Getting that ironed out will more than likely fix your issue.

I would look into a different way to handle your login system as it seems to be messing up the way codeigniter is working. If you want, show us what your modified controller looks like and we can maybe help you otherwise you are pretty much on your own.

Henesnarfel
  • 2,129
  • 1
  • 16
  • 18
0

It looks to me like there is another error causing this, related to 'Message: Undefined property: Users::$session' in your MY_Controller (probably in the constructor).

If you get that fixed, I think the 404 override route will work.

JohnWright
  • 248
  • 1
  • 7