You can also pass the URL as an array: $vars = $this->requestAction() Passing other request data $viewHtml = $this->requestAction('/articles/popular', ) You should use URL formats that are compatible with Router::reverse() ExamplesĪ basic example getting the return value of the controller action: $variables = $this->requestAction('/articles/popular') Ī basic example of request action to fetch a rendered page without the layout. Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. ![]() requestAction can be used to return rendered views or fetch the return value from controller actions. Can be used to connect controllers together or tie plugins into a main application. RequestAction() source public requestAction( string|array $url, array $extra )Ĭalls a controller's method from any location. Cake\Controller\ComponentRegistry|null $components optional null The component registry. Cake\Event\EventManager|null $eventManager optional null The event manager. string|null $name optional null Override the name useful in testing when using mocks. Cake\Network\Response $response optional null Response object for this controller. Can be null for testing, but expect that features that use the request parameters will not work. Parameters Cake\Http\ServerRequest $request optional null To override the conventions CakePHP uses you can define properties in your class declaration. Sets a number of properties based on conventions if they are empty. _construct() source public _construct( Cake\Http\ServerRequest $request null, Cake\Network\Response $response null, string|null $name null, Cake\Event\EventManager|null $eventManager null, Cake\Controller\ComponentRegistry|null $components null ) ![]() afterFilter(Event $event) Called after each action is complete and after the view is rendered.Ĭake\Controller\Controller implements Cake\Event\EventListenerInterface, Cake\Event\EventDispatcherInterface uses Cake\Event\EventDispatcherTrait, Cake\ORM\Locator\LocatorAwareTrait, Cake\Log\LogTrait, Cake\Utility\MergeVariablesTrait, Cake\Datasource\ModelAwareTrait, Cake\Routing\RequestActionTrait, Cake\View\ViewVarsTrait Direct Subclasses.beforeRedirect(Event $event, $url, Response $response) Called before a redirect is done.beforeRender(Event $event) Called before the view is rendered.This is a good place to do general logic that applies to all actions. beforeFilter(Event $event) Called before each action.By implementing a method you can receive the related events. Life cycle callbacksĬakePHP fires a number of life cycle callbacks during each request. ![]() You can re-map URLs using Router::connect() or RouterBuilder::connect(). For example /posts/index maps to PostsController::index(). By default controllers and actions use conventional names. In either case $this->response allows you to manipulate all aspects of the response.Ĭontrollers are created by Dispatcher based on request parameters and routing. This usually takes the form of a generated View, or possibly a redirection to another URL. The request object contains all the POST, GET and FILES that were part of the request.Īfter performing the required action, controllers are responsible for creating a response. You can access request parameters, using $this->request. For example adding or editing a new object, or listing a set of objects. Each action serves as an endpoint for performing a specific action on a resource or collection of resources. These are public methods on a controller that are not inherited from Controller. Provides basic functionality, such as rendering views inside layouts, automatic model availability, redirection, callbacks, and more.Ĭontrollers should provide a number of 'action' methods. ![]() Application controller class for organization of business logic.
0 Comments
Leave a Reply. |