CodeIgniter BASEPATH

2019-06-11 09:44发布

问题:

Hi I am new to Codeigniter. From my understanding of the BASEPATH, it is used to activate the CI framework. The code

defined('BASEPATH') OR exit('No direct script access allowed');

is used at top of the page to prevent direct access to the controller. But it is not working in my code. When I try to directly access a view via controller, the view gets loaded. I have checked index.php and BASEPATH is defined there. Please advice.

回答1:

defined('BASEPATH') OR exit('No direct script access allowed'); is used to make sure that the request has gone through index.php in your root dir. This is for reasons such as making sure that all CI base classes are being loaded and making sure certain vars have been set etc.

So, yes you will be able to access a view file if you're going through a controller.

Hope this helps!



回答2:

defined('BASEPATH') OR exit('No direct script access allowed');
  • defined: Checks whether a given named constant exists
  • BASEPATH: it´s a constant from codeigniter reserved names
  • OR exit prints the string: 'No direct script access allowed'
  • exit — Output a message and terminate the current script.

Other reference: PHP 5 Constants



回答3:

Use this code Before Class start For Example

defined('BASEPATH') OR exit('No direct script access allowed');

class Dataentry extends CI_Controller { }