Creating a “Hello World!” Application with Phalcon Framework PHP

application with Phalcon Framework | In this tutorial we will try to create a “Hello World” with Phalcon Framework PHP. Before using phalcon you have to install phalcon .dll in xampp (Installing Phalcon Framework PHP on Windows).

Create a folder and some files as follows:

Capture1

Open the Phalcon_HelloWorld/.htaccess file and enter the source code below:

RewriteEngine on RewriteRule ^$ public_html/ [L] RewriteRule (.*) public_html/$1 [L]
.htaccess
All requests to the application will be rewritten to the public_html/ directory making it the document root. This step ensures that the system’s internal folders remain hidden from public view and eliminates a security threat to this vulnerabilities.

Open the second file Phalcon_HelloWorld/public_html/.htaccess

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*) index.php?_url=/$1 [QSA,L]
public_html/.htaccess
Then we will create a bootsrap file. This file is very important as it acts as the base of the application giving you all the control.

Phalcon_HelloWorld/public/index.php

<?php

try {

//Register an autoloader
$loader = new PhalconLoader();
$loader->registerDirs(array(
    '../app/controllers/',
    '../app/models/'
))->register();

//Create a DI
$di = new PhalconDIFactoryDefault();

//Setup the view component
$di->set('view', function(){
    $view = new PhalconMvcView();
    $view->setViewsDir('../app/views/');
    return $view;
});

//Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
    $url = new PhalconMvcUrl();
    $url->setBaseUri('/Phalcon_HelloWorld/');
    return $url;
});

//Handle the request
$application = new PhalconMvcApplication($di);

echo $application->handle()->getContent();

} catch(PhalconException $e) {
echo “PhalconException: “, $e->getMessage();
}
public_html/index.php
Creating a controller by default Phalcon will look for a controller named “Index”. This is the starting point when no controller or action is requested in the request. Create a new file named IndexController.php in the folder Phalcon_HelloWorld/app/controllers/IndexController.php

<?php

class IndexController extends PhalconMvcController
{

public function indexAction()
{
    echo "<h1>Hello World!</h1>";
}

}
app/controller/IndexController.php
The controller class must end in “Controller” and the actions on the controller must end in “Action”. If you have finished following all the tutorials above, the folder arrangement will look like this:

Capture2

Now let’s see in the browser, if it is correct it will look like this http://localhost:8080/Phalcon_HelloWorld/

Untitled1

Congratulations, you have finished learning the basics of the phalcon framework 😀