Cakephp Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9|Last
Lessons:-Controllers

Controller actions
Controller actions are responsible for converting the request parameters into a response for the browser/user
making the request. CakePHP uses conventions to automate this process and remove some boiler-plate code
you would otherwise need to write.
By convention CakePHP renders a view with an inflected version of the action name. Returning to our online
bakery example, our RecipesController might contain the view(), share(), and search() actions.
The controller would be found in /app/Controller/RecipesController.php and contain:
# /app/Controller/RecipesController.php
class RecipesController extends AppController {
public function view($id) {
//action logic goes here..
}
public function share($customerId, $recipeId) {
//action logic goes here..
}public function search($query) {
//action logic goes here..
}
}
The view files for these actions would be app/View/Recipes/view.ctp,
app/View/Recipes/share.ctp, and app/View/Recipes/search.ctp. The conventional
view file name is the lower cased and underscored version of the action name.
Controller actions generally use set() to create a context that View uses to render the view. Because of
the conventions that CakePHP uses, you don’t need to create and render the view manually. Instead once a
controller action has completed, CakePHP will handle rendering and delivering the View.
If for some reason you’d like to skip the default behavior. Both of the following techniques will by-pass the
default view rendering behavior.
• If you return a string, or an object that can be converted to a string from your controller action, it will
be used as the response body.
• You can return a CakeResponse object with the completely created response.
When controller methods are used with requestAction() you will often want to return data that isn’t
a string. If you have controller methods that are used for normal web requests + requestAction you should
check the request type before returning:
class RecipesController extends AppController {
public function popular() {
$popular = $this->Recipe->popular();
if (!empty($this->request->params[’requested’])) {
return $popular;
}
$this->set(’popular’, $popular);
}
}
The above controller action is an example of how a method can be used with requestAction()
and normal requests. Returning an array data to a non-requestAction request will cause errors and
should be avoided. See the section on Controller::requestAction() for more tips on using
requestAction()
In order for you to use a controller effectively in your own application, we’ll cover some of the core attributes
and methods provided by CakePHP’s controllers.

 
 
 

osdyui

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)