2

I am building an app in codeigniter and my application folder hierarchy goes like this:

|-controller
   |-admin
      |-dashboard

I want to add dashboard to the namespace admin, so I did this:

defined('BASEPATH') OR exit('No direct script access allowed');
namespace admin;
class Dashboard extends MY_Controller {
     //constructor and some methods
}

The problem is that whenever i try to access the dashboard class from uri localhost/myapp/admin/dashboard, I get error on the browser like this:

Server error

500

However if I comment out the line namespace admin; , it runs without any errors.

Why is the namespace creating such internal server error?

How to prevent such error while using namespaces for controller class in codeigniter?

Birendra Gurung
  • 2,168
  • 2
  • 16
  • 29

2 Answers2

1

The reason for this is Codeigniter does not natively support namespaces. They did this to allow backwards compatibility of Codeigniter for PHP 4. Namespaces only became available in PHP 5.3.0 and later versions.

Jon
  • 86
  • 10
1

First check which version of PHP and codeigniter you are using and let us know .

CodeIgniter does not support namespaces depending on version. If you want to use namespaces php version must be >= 5.3.0 Codeigniter doesn't use namespaces because it is written to support php 4

If you must have to use namespaces then I am linking you to Stack Overflow Similar Problem link that has Upvoted reference for using Namespace with Codeigniter By making configuration editing. You can try that but by default it does not support it

Community
  • 1
  • 1
Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
  • For more reference You can see things added to CodeIgniter 3 in link given below https://www.reddit.com/r/PHP/comments/30u0w9/codeigniter_3_is_out/ – Heemanshu Bhalla Feb 19 '16 at 17:06