posts_controller.php
inside the /app/controllers
directory. Here's what the basic controller should look like:class PostsController extends AppController {Now, lets add an action to our controller. Actions often represent a single function or interface in an application. For example, when users request www.example.com/posts/index (which is also the same as www.example.com/posts/), they might expect to see a listing of posts. The code for that action would look something like this:
var $name = 'Posts';
}
?>
class PostsController extends AppController {You may be tempted to name your controllers and actions a certain way to obtain a certain URL. Resist that temptation. Follow CakePHP conventions (plural controller names, etc.) and create readable, understandable action names. You can map URLs to your code using "routes" covered later on.
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
}
?>
Let me explain the action a bit. By defining functionindex()
in our PostsController, users can now access the logic there by requesting www.example.com/posts/index. Similarly, if we were to define a function calledfoobar()
, users would be able to access that at www.example.com/posts/foobar.
The single instruction in the action uses set()
to pass data from the controller to the view (which we'll create next). The line sets the view variable called 'posts' equal to the return value of the find('all')
method of the Post model. Our Post model is automatically available at $this->Post
because we've followed Cake's naming conventions.
To learn more about Cake's controllers, check out Chapter "Developing with CakePHP" section: "Controllers".
No comments:
Post a Comment