Doulat Meah

0
Blog, it is today a web classic "hello world" of web solutions. Let's take a quick look at how to create such a simple blog in the net.
Requirements :
Understanding the basic principles Nette (relations in the MVP pattern, the basic idea of ​​the presenter and his relationship to the templates)
Introduction
Creating a simple blog is an evergreen topics tutorials various web frameworks. Unfortunately, most of these tutorials is curtailed to the minimum spanning tree of the task and the main objective is to show the user how to easily Framework, it goes with that. This reduces the complexity of the task at the level at which almost worth a framework to use. So do not take this tutorial as a teaching material.
tutorial is used in namespace version Nette. Although the application is so small that namespaces are there more of a nuisance, it's something that every potential user Nette should know and use. If for technical reasons, can not use PHP version => 5.3, use overrefined Nette version for PHP 5.2 and the drain of said source code definition namespaces.

Blog for 19 minutes!
With RoR or Code Ignite can create a "complete" "blog" for "20" minutes. Dear, it's nothing! We will show you that with Nette's 19 minutes you can too!

Installation


We use the skeleton of distribute. Copy it to a folder on the server. In the folder / libs will copy dibi (sold Nette is located in the folder 3rdParty). We will also need a database, I'll stick to MySQL, but if you want to use SQLite or PostgreSQL, this is not a problem, just read on.

Remember that the skeleton is missing Nette in the folder / libs !

Database and model


Let's start with creating the appropriate tables. Their structure is clear from the brief. So run on our database the following commands:

CREATE  TABLE  `posts` (
`id` int ( 11 ) NOT NULL AUTO_INCREMENT,
`title` varchar ( 128 ) COLLATE utf8_bin NOT NULL ,
`body` text COLLATE utf8_bin NOT NULL ,
`date` datetime NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_bin;

This will create a table with articles.


CREATE  TABLE  `comments` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`post_id` INT NOT NULL ,
`author` VARCHAR ( 128 ) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
INDEX (post_id),
FOREIGN KEY (post_id) REFERENCES posts(id)
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_bin;


And this (surprisingly) a table of comments.

If you are using a different database, create the same table structure, dibi takes care of compatibility on the part of the application itself.

When we have a database, you need to connect it through dibi. Configuration files in Nette offer an elegant way to manage data such as information about the database. Open config.ini in the folder / app and parts [common] Add the appropriate modification of these lines:


db.server = localhost
db.database = blogtut
db.username = blogtut
db.password = blogtut
db.driver = mysqli
db.charset = utf8
db.lazy = TRUE





And now the connection itself. To file bootstrap.php (boot file of the entire application, as you should know), add a line


$ Application -> run () / / This line runs our application ...


following:

dibi :: connect (Environment :: getConfig ( 'db' )) / / ... and connect to need before you start


You stop for a moment and give you a minute theory, which deepen knowledge and to answer possible questions. A few key points:


$db = array (
'server' => 'localhost' ,
'database' => 'blogtut' ,
'username' => 'blogtut' ,
'password' => 'blogtut' ,
'driver' => 'mysql' ,
'charset' => 'utf8' ,
'lazy' => TRUE
);


  • Even if we set the database connection each time the application starts, the connection is real only when it is really needed, thanks to the setting db.lazy = TRUE in config.ini .


We have yet to develop models for both tables.

<? Php

class PostsModel
{
public static function fetchAll ()
{
return dibi :: fetchAll ( '
SELECT *
FROM [posts]
ORDER BY [date] ' , dibi :: DESC
);
}
}

<? Php

class CommentsModel
{
public static function fetchAll ( $ post_id )
{
return dibi :: fetchAll ( '
SELECT *
FROM [comments]
WHERE [post_id] =% i ' , $ post_id
);
}
}

For clarity, the moment will their methods without undue respects to pull all available data. Both sets of class definitions saved in / app / models and name them according to the class that contains ( PostsModel.php , CommentsModel.php ).

If you are used to terminate scripts brand ?> and quickly weaned off! end tags are optional and only cause problems with non-printing characters, which then causes the inability to send the HTTP headers.

Presenter


/ app / presenters / is HomepagePresenter. It will serve as a good basis for our efforts. Add to it a method that takes data from the model and passes them to the template to render.
renderDefault method to adjust

public  function renderDefault ()
{
$ This -> template-> posts = PostsModel :: fetchAll ();
}

If you are unsure about why working with a default method in the presenter Homepage, look for the routes in the bootstrap.php file.

View


Well, now we view default on the variable $ posts , which contains all the posts. Let's dump.
folder / app / templates file is @ layout.phtml . It contains the basic framework of all the sites that we create. Therefore, I suggest you look at it.
/ app / templates / Homepage is set default.phtml that contains the block definition content , the content of which will replace the {include} # content in the layout. List of all articles might look like this:

{ block content } 
<h1> My blogísek </h1>
<div id= "posts" >
{ if count( $posts ) }
{ foreach $posts as $post }
<div class= "post" >
<h3> { $post [ 'title' ] } </h3>
<small> Přidáno { $post [ 'date' ]|date } </small>
<p> { $post [ 'body' ] } </p>
</div>
{ / foreach }
{ else }
There was no written article.
{ / if }
</ div>

Download test data , load them into the database and try to open the root site in your browser.The result should look like this:

 

Post a Comment

 
Top