Home » Phalcon First Example

Phalcon First Example

by Online Tutorials Library

Phalcon First Example

Bootstrap

To create any app in phalcon, firstly you have to create a bootstrap file. Bootstrap file acts as the entry-point and configuration of the application. This file handles 3 things:

  1. Registration of component autoloaders.
  2. Configuring Services and registering them with the Dependency Injection.
  3. Resolving the application’s HTTP requests.

To create this file we follow four steps:

  1. Create Autoloaders
  2. Use Dependency Management
  3. Create Factory Default
  4. Handling Application Request

Autoloaders

It loads the PSR-4 which consists of complaint file loader which is running through Phalcon C extension. Autoloaders add mostly Controllers and Models. We can also register directories consist of different file namespace.

Public/index.php

Phalcon First Example 1

Dependency Management

Phalcon is loosely coupled framework due to which all services are registered with dependency manager. All services are delivered automatically to components and services inside the IoC (Inversion of Control) container.

All services are included in a class file DI (Dependency Injection) and IoC container consists of following concepts:

  • Service Container: a “bag” where we globally store the services that our application needs to function.
  • Service or Component: Data processing object which will be injected into components.

Directory: PhalconDI

Factory Default

Factory Default i.e. PhalconDiFactoryDefault is the variant of PhalconDi.

First we create the object for the DI which automatically registers most of the component.

publicindex.php

Phalcon First Example 2

Now, we register the “view” services with the directory address where to find the files.

Phalcon First Example 3

Now, we generate the base URL

Phalcon First Example 4

Application Request

Application request implements MVC (Model View Controller) for the application. Under this we initialize 3 things:

  1. Initialize request environment.
  2. Route the incoming request.
  3. Dispatch any discovered actions.

publicindex.php

Phalcon First Example 5

Example

Complete bootstrap file:

Output:

Phalcon First Example 6

Next TopicPhalcon Cache

You may also like