<p /> So, we decided to start programming “by concepts” and finally deal with some kind of framework.
<p /> A painful choice arises before us, what kind of framework for this to use. To do this, you can find a bunch of articles, reviews, benchmarks for speed, performance, usability and installation, or you can simply type in the "php framework" in Google and click
"I'm lucky."
<p /> Hooray, we have just chosen to develop a framework called CakePHP!
Of course, we don’t have time to read about its advantages and disadvantages, we need to implement our project as quickly as possible. Therefore, we write a tutorial in a search and get into an article about
how to stick a small blog in 5 minutes .
<p /> This article begins with a description of how to install CakePHP in your project. Tells about what you need to keep in each daddy and why they are needed.
Then he goes straight to the point, invites you to create a table, set up access to MySQL (for some reason, in that order, and not vice versa), check whether the mod_rewrite is configured correctly in the Apache. And then offers to start creating a blog.
This blog is a table that contains posts. We can add, edit and delete posts. It should be noted that all this is really very simple. Almost as simple as on the rails.
<p /> This article is written in simple and understandable English and contains all the code needed to create a blog. Know yourself copy, yes paste.
<p /> But creating this blog on this tutorial, we suddenly realize that we don’t have the most important thing for which all blogs are kept. These are kamenty (or “comments” for lovers of the purity of the language). In a panic, we begin to google, looking for an extended tutorial, but we are convinced that apart from the components of transcendental complexity, we cannot find anything. And then we decide on a desperate step: add the functionality of the kaments themselves.
<p /> To do this, first create a table in which kament will be stored
CREATE TABLE `comments` (
`id` int(11) NOT NULL auto_increment,
`post` int(11) default NULL,
`comment` text NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `comments` VALUES (1, 1, 'This is a comment to the first post.', NOW( ), '0000-00-00 00:00:00');
INSERT INTO `comments` VALUES (2, 2, 'The comment to second post.', NOW( ), '0000-00-00 00:00:00');
<p /> This request created the comments table for us and added two comments to it. For the first and second entries, respectively.
<p /> Now we need to make these kamenty visible. We heard about MVC, so we immediately go to the post controller (posts_controller.php). And we add the following line there:
var $uses = array('Post', 'Comment');
<p /> The $ uses variable of the controller is responsible for the models that the controller will use. If this variable is not set, then by default it will use only the model whose controller it actually is. That is, PostsController will only use Post.
<p /> Here, as you can see, we indicate to the controller that it will use the Post and Comment models.
<p /> Now we’ll edit the function of the controller, which is responsible for displaying the actual post.
function view($id)
{
$this->Post->id = $id;
$this->set('post',$this->Post->read());
// :
$this->set('comments' , $this->Comment->findAllByPost($id));
}
<p /> With this new line, we add a new comment variable to the view and set its value to the $ this-> Comment-> findAllByPost ($ id) result.
<p /> Here I must say a few words about the great findAllByPost method.
<p /> Smart CakePHP provides classes inherited from AppModel to use methods of type findAllBy <field name> (<value>). Who will find all records from the table corresponding to the model, where <field name> = <value>. Great, right?
<p /> Therefore, $ this-> Comment-> findAllByPost ($ id) will return all emails belonging to the displayed post.
<p /> Now in the view.thtml file, which is located in the app / views / posts folder, we will add a display of these features. To do this, at the very end of this file add:
<?php
foreach($comments as $comment):
echo ''.$comment['Comment']['comment'].'<br/>';
endforeach;
?>
<p /> Here I’ll scroll through all the pages we’ve passed to the sideview in the previous piece of code and display them on the screen. Cool? Cool!
<p /> But in kamenta the most important thing is that they can be left. Therefore, we need a form for adding kamentov at the bottom of the post. The form helper will help us with this.
var $ helpers = array ('Html', 'Form');
<p /> This line in the posts_controller.php file tells the controller which helpers it will use in its work. Html and Form help our post controller respectively.
<p /> Add the following to the same view.thtml file:
<?php
echo $form->create('Comment');
echo $form->hidden('post', array('value' => $post['Post']['id'])); echo $form->input('comment', array('type'=>'textarea'));
echo $form->submit('Submit');
echo $form->end();
?>
<p /> This is our little helper created a form of adding a stone. In it, the post id is specified as a hidden field (hidden), and a user is asked to enter a textarea.
<p /> Now we need something that can control the addition of this kament. This will be the comment controller:
<?php
class CommentsController extends AppController
{
var $name = 'Comments';
function add()
{
if (!empty($this->data))
{
if ($this->Comment->save($this->data))
{
$this->Session->setFlash('Redirecting');
$this->redirect('/posts/view/'.$this->data['Comment']['post']);
}
else
$this->Session->setFlash('You fail');
}
}
}
<p /> There is only one handler for one add event in the controller controller. It is identical to the same handler for adding a post controller, only redirects to another place.
<p /> Hooray! We now have a real blog with kamentami. But not simple, but written on a real framework, according to the true MVC architecture. With my own hands.