Last Updated:

Creating a blog on Phalcon Framework

Phalcon


Lesson 1. Initial launch of phalcon. Create a project.



The manual was created on the linux operating system.
Please note that for phalcon to work we will need php versions higher than 5.3 and included mod_rewrite on apache.

For development, we will need to install phalcon dev-tools.

Let's assume that the Phalcon Framework is already downloaded and configured.
If not, read the article (installing and configuring the phalcon framework).


Create a project on a virtual host.


This article discusses a virtual host named pacificsky.blog.


Go to the directory with the sites of your apache.
Standard Directory:


How do I configure virtual hosts?

If a virtual host is created, then go to it.

Open the terminal (console). Go to the directory of your sites.
Default Directory:


Project Name pacificsky.blog.
In the terminal you need to specify:


Go to the newly created folder:

Location:


Using Phalcon dev-tools, create a project where your site will be located:
In the terminal we write:

This command creates a new project on Phalcon in the folder public_ html.

If the project is created. The terminal will display the following message:


The project created with dev-tools should contain the following files:
/var/www/
cd /var/www/
mkdir pacificsky.blog
cd pacificsky.blog
/var/www/pacificsky.blog/
phalcon project public_html

Success: Controller "index" was successfully created.                       
Success: Project "pacificsky.blog" was successfully created.


app
index.html
.htaccess
public


The app contains the site logic. Public contains public files that are available to everyone.

Now open the browser and go to the virtual host of the local site.
url line write


Browser will display the following text:
http://pacificsky.blog/


Congratulations!

You're now flying with Phalcon. Great things are about to happen!
This page is located at views/index/index.phtml


Congratulations your phalcon project is installed and running!

Create a project without a virtual host.



Open the terminal (console). Go to the directory of your sites.
Default directory:


Using Phalcon dev-tools, create a project where your site will be located:
In the terminal we write:

If the project is created. The terminal will display the following message:


Go to the folder with phalcon.


Now transfer all the files from the pscificsky.blog folder to the directory

Using the following command:


Now we have files in the www directory:
/var/www/
phalcon project pacificsky.blog

 Success: Controller "index" was successfully created.                       
 Success: Project "pacificsky.blog" was successfully created.
cd phalcon.blog
/var/www/
sudo cp * /var/www



app
index.html
.htaccess
public
pacificsky.blog



Delete the unnecessary directory pacificsky.blog


Now the www directory contains the following files:
sudo rm -R pacificsky.blog



app
index.html
.htaccess
public


The app contains the site logic. Public contains public files that are available to everyone.

Now open the browser and go to the virtual host of the local site.
url line write


Browser will display the following text:
http://localhost/


Congratulations!

You're now flying with Phalcon. Great things are about to happen!
This page is located at views/index/index.phtml


When a Volt directory error occurs, can't be written



Go to project


Issue 777 rights to the public


folder and 751 rights to the views


folder Congratulations your project on Phalcon is installed and running!
cd /var/www/pacificsky.blog/public_html
chmod -R 777 public
chmod -R 751 /var/www/pacificsky.blog/public_html/app/views

 

Lesson 2. MVC. Input, output of data from the database.


Initially, you must create the database. This article uses the name BD psblog.

You must create a table named posts.

Fill it in with 3 fields:


id - with parameters AUTO_INCREMENT and index primary
title - varchar type size 255
body - type text



Request to create a table with the necessary fields:


In the phalcon project, you need to open the configuration file:


As standard, it contains the following configuration:



You need to fill in the configuration fields according to your web server:


Attention! All fields must be filled in correctly. Otherwise, the connection to the database will not be established.
After setting up the configurations: save the data and close the file.
Let's start creating a model.

You need to go to the root of the Phalcon project.


Create a model file of the same name as the table in the database.


If the command is successful, the following message will be displayed in the terminal:


CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `body` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
sudo nano app/config/config.php

<?php

defined('APP_PATH') || define('APP_PATH', realpath('.'));

return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '',
        'dbname'      => 'test',
        'charset'     => 'utf8',
    ),
    'application' => array(
        'controllersDir' => APP_PATH . '/app/controllers/',
        'modelsDir'      => APP_PATH . '/app/models/',
        'migrationsDir'  => APP_PATH . '/app/migrations/',
        'viewsDir'       => APP_PATH . '/app/views/',
        'pluginsDir'     => APP_PATH . '/app/plugins/',
        'libraryDir'     => APP_PATH . '/app/library/',
        'cacheDir'       => APP_PATH . '/app/cache/',
        'baseUri'        => '/',
    )
));
'adapter' => 'Database type',
'host' => 'Database host',
'username' => 'Your username to Mysql',
'password' => 'Your MySQL password',
'dbname' => 'dbname',
'charset' => 'Database character set',
phalcon model posts

 

Success: Model "Posts" was successfully created.



And the project will create a model to work with the database table of the same name:


Let's start creating a controller. In the terminal in the root of the project, run the following command:


If the command is successfully executed, the following message will be displayed in the terminal:


Leave the model file unchanged. Let's get started with the controller:


Controller content:


The database is currently empty. It contains only the posts table. Let's start by writing the action responsible for creating new entries in the posts table. Let's name the action addAction and add its skeleton.



Let's start populating the addAction.
You need to check whether the post request came:

Create a new instance of the model and assign it option:


Accept Post requests.

Save to database:

Redirect to the index:


Create view action.

In folder:

You need to create a new folder with the same name as the controller:

app/models/Posts.php
phalcon controller posts
Success: Controller "posts" was successfully created.
sudo nano app/controllers/PostsController.php
class PostsController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        
    }
}

class PostsController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        
    }

    public function addAction()
    {

    }
}
if($this->request->isPost()) {
$option = new Posts();

$option->title = $this->request->getPost('title');
$option->body = $this->request->getPost('body');
$option->save();

return $this->dispatcher->forward(
     array(
               "action" => "index"
     )
);
app/views/

 

posts


In the posts folder, create a file named like the add action with the extension volt:

volt - a template engine built into the phalcon framework.

In add.volt we write:


{{ form("method":"post") }} - creates a form with the post method.
{{ text_field('title') }} - creates a fillable field named title.
{{ text_field('body') }} - creates a fillable field named body.
{{ submit_button('Save new data') }} - creates a button
{{ link_to('/', "Go on head page") }} - creates a link to move to the main page.

Type the following url in your browser:


Create a pair of entries on the page. Open the posts table in your database. You should see the records you created.

Let's start creating an activity to display tabular data.

Open postsController.php and in the indexAction activity, type instance:


Controller seeding complete. Let's start creating a view again.

In the directory:

Create an index.volt

file The index.volt file should consist of the following lines:
Run through our array of records, which is contained in the posts variable.
This loop is somewhat reminiscent of foreach:

Display the title and body fields that are contained in the post:

Complete loop:


The code should take the following form:


In the browser, write the url line:


Today we analyzed the creation of a model, a view, and a controller. As well as writing and reading data from the database.

add.volt

<h1>Add new post</h1>

{{ form("method":"post") }}
    <p>
        Title: {{ text_field('title') }}
        Body: {{ text_field('body') }}
    </p>
    <p>
        {{ submit_button('Save new data') }} - 
        {{ link_to('/', "Go on head page") }}
    </p>
{{ end_form() }}
http://имя вашего сайта/posts/add
$this->view->posts = Posts::find();

    public function indexAction()
    {
        $this->view->posts = Posts::find();
    }
app/views/posts
{% for post in posts%}

[h3]{{ post.title }}[/h3]
<p>{{ post.body }}</p>
{% endfor %}

<h1>Posts</h1>

{% for post in posts%}
    <h3>{{ post.title }}</h3>
    <p>{{ post.body }}</p><br />
{% endfor %}
http://your site name/posts/