-3

I have a custom mvc app which has an index controller and an index model.The url for controllers in this app is $url[0]. The system is designed to redirect to index when the value for controller($url[0] is empty. e.g., When the url is http://localhost/Test/ The system should automatically redirect to the index.

However, when i include a model method in the controller i get the following error:

Notice: Undefined property: Index::$model in Fatal error: Call to a member function userList() on a non-object

tapeli
  • 79
  • 1
  • 1
  • 7

2 Answers2

0

The problem is in the bootstrap.php file line 21 to line 27. You say that when the url is http://localhost/Test/index the app works as expected but when $url[0] is empty i.e., http://localhost/Test you get the error after explicitly stating that if url[0] is empty then the system should automatically redirect to index controller. Try amending your bootstrap by introducing more logic

tom sawyer
  • 47
  • 1
  • 2
  • 9
  • Thanks for your suggestion. I have looked at the logic in lines 21 to 27 as you have suggested and think that i should not tamper with it since it works well. I highly suspect that my design is faulty and that index controllers were never meant to display data in PHP – tapeli Mar 02 '18 at 09:36
0

After much deliberation and thinking, i figured that the only way to avoid this error is to change one line on bootstrap.php. i replaced the $Controller->Index(); with a header location call like this header('Location: ' . index). This is due to the fact that setting the controller to $controller->Index() in the bootstrap overrides the Index action/method defined in the index controller(index.php) hence stopping the controller from loading the model.

This way the page will always redirect to http://localhost/Test/index when the url[0] is empty.

tapeli
  • 79
  • 1
  • 1
  • 7