Tutorial by Examples

namespace App\Controller; class PostsController extends AppController { public function initialize(){ parent::initialize(); // code that you want to run before every action } public function view($id) { //Your code here } }
The beforeFilter() method executes before any other method executes in the controller. First use the Event namespace before defining the class in your controller file. use Cake\Event\Event; In your controller, add the beforeFilter() method as shown below. public function beforeFilter(Event $ev...
Pass each variable to view at a time $this->set('color', 'pink'); $this->set('color', $color); Pass multiple variables to view together via compact() function $color1 = 'pink'; $color2 = 'red'; $this->set(compact('color1', 'color2'));
You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key. $this->request->data['field']; Retrieve specific key value $this->request->data('key_name'); Retrieve specific key value of nested array $this->requ...
By default CakePHP loads the related model in the controller. In order to load another model in the controller, use the loadModel() method: $this->loadModel('Articles'); or load on the fly $table = TableRegistry::get('Articles'); $table->find();
Redirect to within application (another action of specific controller). return $this->redirect([ 'controller' => 'myController', 'action' => 'myAction' ]); Redirect to referrer page return $this->redirect($this->referer()); Redirect to outside of application or spec...
Passing Variable in URL as a method's parameter return $this->redirect([ 'controller' => 'users', 'action' => 'profile', $id ]); Url should be looks like this http://your_app_url/users/profile/{id} in UsersController.php file in profile() method class UsersController e...
Set default layout for entire application. i.e, created layout file in /src/Template/Layout/admin.ctp class AppsController extends Controller { public function beforeFilter(Event $event) { parent::beforeFilter($event); $this->viewBuilder()->layout('admin'); // For Ver...
Generally in AJAX request no need of load CSS, JS. Also omitting other HTML code. Make ajax.ctp file in /src/Template/Layout, and code should be <?php $this->fetch('content'); Set AJAX based layout for entire application, in AppsController.php class AppsController extends Controlle...
We can load components in two ways. By initialize or override $components property in Controller By using loadComponent() method in initialize() method of Controller. Way-1 It should be override loading component by AppsController.php load one or more component class UsersController extend...
initialize() is introduced in CakePHP version > 3.0 As a code structure, it looks like same as beforeFilter() method. but there is many differences between beforeFilter() and initialize(). initialize() is always called after constructor is called. but beforeFilter() is not calling in case of ...
You can retrieve query string data as Array. $post_data= $this->request->query; You can retrieve post data for particular key. $this->request->query['field']; Retrieve specific key value $this->request->query('key_name'); Retrieve specific key value of nested array $th...
How to create User Model Class namespace App\Model\Table; use Cake\ORM\Table; class UsersTable extends Table { public function initialize(array $config) { $this->table('users'); //define table name $this->displayField('username'); // unique or other special field of...
There are 4 types of associations(relationships) we can define in CakePHP class PostsTable extends Table { public function initialize(array $config) { // table initialization code should be here $this->belongsTo('Authors', [ 'className' => 'Authors', ...

Page 1 of 1