I am working with cakephp , i need some clarification regarding initiating.
which file will be loaded first in cakephp whether index.php or bootstrap file
The answer of Ganesh is not really correct. It does not redirect and it does not load AppController as third file, nor does it load it ever directly.
First you should always configure your site to be accessed from the app/webroot folder NOT the index.php in the level above, because if you do that you expose the whole app structure to the public web as well.
When app/webroot/index.php is accessed CakePHP sets a bunch of constants like the CAKE_CORE_INCLUDE_PATH, WWW_ROOT and a few others, the best is to have a look at this file.
Then it will include the bootstrap.php file.
At the end of this file you'll see that not AppController but the Dispatcher is called first and the Request/Response classes are passed to it.
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
See Dispatcher::dispatch().
Then, still no controller is loaded. It first fires events and by this dispatcher filters which can interrupt the request and already send data back to the client. That's how the AssetDispatcher works for example. Again, still no controller here.
If the filters passed then the dispatcher will call the controller that matches the requested url, not AppController, if you called /users/index it will instantiate the UsersController and call its index() method. See Dispatcher::_loadController().
All your Controllers should extend AppController but AppController actually never gets called directly.
When a cakephp application is accessed first it will load the index.php from there it redirects to bootstrap.php and then AppController.php.
Related
I have installed CakeDC's Users plugin and I have loaded all the required files in bootstrap.php, as shown below:
CakePlugin::load('Users'); // Loads the Users plugin
CakePlugin::load('Search'); // Loads the Search plugin
CakePlugin::load('Utils'); // Loads the Utils plugin
CakePlugin::load('Users', array('routes' => true));
I would like to override the add() action of the UsersController of the plugin, so I created the AppUsersController.php file in app/Controller, as instructed here:
https://github.com/CakeDC/users#extending-the-controller
Then I created an add() action inside the AppUsersController.php, with an empty body, but the original action was not overridden. What am I doing wrong?
Don't create an empty add() on the AppUsersController, because it still load the parent function. Try to copy paste add() from UsersController, and then paste into AppUsersController, then override it
I am developing an application in CakePHP 1.3. Everything is working fine but one error is there.
When I use redirect() in my .ctp page it gives error
Fatal error: Call to undefined method DebugView::redirect()
As redirect is working fine when I use it in controller.
I have included helpers as follows in my AppController
var $helpers = array('Html', 'Form','Session');
Please help
thanks in advance
You never redirect (or output any header if possible) in the view layer.
Use the controller to do so.
The view then should only render the output according to the desired output format (html, xml, json, ...). Header stuff is part of the reponse and responsibility of the controller (and in 2.x the response class itself).
So your observation that it will work with controllers and not inside views is correct.
"Call to undefined method" always is a good indicator for a method not being available in this scope.
Redirects being part of the "logic" makes them only available in controllers and components. Never ever in the view (output after all logic happened).
In your controller:
$this->redirect( 'url, absolute or relative here' );
Done.
I followed Andrew Perkins excellent tutorial on setting up permissions in CakePHP 2.0.
My question, however, relates to how to use the allow and deny method in the Pages controller. Currently I have $this->Auth->allow('display') which allows all methods in the Pages controller to be view.
What if I only want the home page allowed but the rest denied? How do I code that?
Thanks in advance.
Make sure you have copied the PageController.php to your app/Controller folder. Then, add a beforeFilter callback method and set access based on the passed page parameter:
public function beforeFilter() {
// Use $this->request->pass to get the requested page name/id
// Decide on access with $this->Auth->allow()
}
This should solve your problem.
You can find more information on request's lifecycle in CakePHP manual. That's pretty useful stuff.
Have you tried this code?
You can out it into your PageController or into your Controller directly
$views = array ('index'); //array of view that you want allow
$this->Auth->allow($views);
I've run into a problem in my CakePHP app as a result of adding some ajax actions.
My table is called orders so obviously my controller is called OrdersController and the model is called Order
It is my understanding of CakePHP's best practices that if I am going to run any logic on the Order model, that it should be done in the OrdersController. This is fine for the basic CRUD stuff but now that some of my views need to send ajax requests to manipulate Order data I have a problem.
The problem is that for ajax to work properly, I have to put this at the beginning of the OrdersController
var $layout = 'ajax'; // uses an empty layout
var $autoRender=false; // renders nothing by default
Then, to stop the security component interfering with my Ajax form submissions, I also need this:
public function beforeFilter() {
parent::beforeFilter();
$this->Security->csrfUseOnce = false;
$this->Security->csrfExpires = '+1 hour';
}
None of this would be a problem if the controller was only being used for Ajax requests, but the problem is that it's being used for regular Cake actions too.
Is the answer that I should have two controllers? One for regular actions and one for ajax actions? This doesn't seem to be mentioned in the Cake docs and it doesn't seem like a very efficient way of doing things.
I know I can change the layout and possibly the auto-render setting on a per-action basis, but I don't see how it's possible to do this with the csrf settings, which need to be in the beforeFilter.
No need for a separate controller. Use cakes request handler. In your controller method, you can test if it's an Ajax request.
if ($this->request->is('ajax')) {
//set to Ajax layout and security settings, etc
You'll need to include the request handler component at the top of your controller:
public $components = array('RequestHandler');
See this page in the cook book For more info: http://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html
I have made a helper class called Navigation that gets used on every page because it does stuff to my main navigation menu. So in order for this to work I have included the helper in my pages controller like so:
var $helpers = array('Html', 'Javascript', 'Navigation');
however when there is an error like a missing view or something the helper can't be found and I get a reference to a non-object error that messes my page layout up. I'm guessing this is becuase an error page uses a different controller, however there isn't a file error_controller.php or anything in the controllers file. So my question is where do I need to declare the helper so it can be found by an error page. Would I need to make an error controller file or is there already a file that I can add it in to?
Any help would be much appreciated
Thanks
If it's used on every page, why not add it to the AppController?