Hi Readers,
Welcome to FindNerd, today we are going to discuss how to set a variable in CakePHP in 2.4.1 version?
The set() method is a very important feature for view presentation. This method provides to set a value in our working controller for our view page.
In CakePHP, views should have a same name as the function name.
So, if you are using CakePHP for making any web application then you have to use set() method, because for the view the set() method is best way to send data from your controller. If you use set() method, then the variable can be accessed in your view.
This method also accepts an associative array as its first parameter.
Syntax of set() method
$this->set('variable','value')
In below example you can see there is a signup() function in which we are using set() method the layout of signup view page and also set subcategories.
<?php
class UsersController extends AppController {
//signup function for layout
public function signup() {
if ($this->Auth->user('id') > 0) {
$this->redirect(array('controller' => 'pages', 'action' => 'home'));
}
//find the list of subcategory
$subcategories = $this->Post->find('all');
//set the variable for view the page
$this->set('subcategories', $subcategories);
$this->set('title_for_layout', SITE_TITLE . '-SignUp');
}
}
?>
0 Comment(s)