Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Learning Phalcon PHP
  • Table Of Contents Toc
  • Feedback & Rating feedback
Learning Phalcon PHP

Learning Phalcon PHP

By : Rada
close
close
Learning Phalcon PHP

Learning Phalcon PHP

By: Rada

Overview of this book

Phalcon is a full-stack PHP framework implemented as a C extension. Building applications with Phalcon will offer you lower resource consumption and high performance whether your application runs on a Linux machine or a Windows one. Phalcon is loosely coupled, allowing you to use its objects as glue components based on the needs of your application. Phalcon PHP's mission is to give you an advanced tool to develop faster websites and applications. This book covers the most common and useful parts of PhalconPHP, which will guide you to make the right decisions while developing a Phalcon-driven application. You will begin the journey by installing and setting up Phalcon for your environment followed by the development of each module. You will be introduced to Phalcon's ORM and ODM. Furthermore, you will also be able to create the first models and database architecture for your project. You will then cover command-line applications, API module, volt syntax, and hierarchical views. Installing and working with Node and Bower for assets management will also be covered. Finally, you will gain insights into creating the backoffice and frontend module along with best practices and resources for development with Phalcon PHP. By the end of this book, you will be able to confidently develop any kind of application using the Phalcon PHP framework in a short time.
Table of Contents (12 chapters)
close
close
4
4. Database Architecture, Models, and CLI Applications
11
Index

The Apache and Nginx configuration files

We will use /var/www/learning-phalcon.localhost as the default directory for our project, and we will refer to it as the root folder. Please create this folder:

$ sudo mkdir -p /var/www/learning-phalcon.localhost/public

Of course, if you want, you can use another folder. Let's create a test file in our public folder under the root directory with some PHP content:

$ cd /var/www/learning-phalcon.localhost/public
$ echo "<?php date();" > index.php

Apache

Let's switch to the default directory where Apache holds the configuration files for the available websites, using the command line: $ cd /etc/apache2/sites-available/. After that, perform the following set of steps:

  1. Using your favorite editor, create a file named learning-phalcon.localhost for apache version < 2.4 or learning-phalcon.localhost.conf for apache version >= 2.4:
    $ vim learning-phalcon.localhost.conf
    
  2. Now, paste the following content to this file:
    <VirtualHost *:80>
        DocumentRoot "/var/www/learning-phalcon.localhost"
        DirectoryIndex index.php
        ServerName learning-phalcon.localhost
        ServerAlias www.learning-phalcon.localhost
    
        <Directory "/var/www/learning-phalcon.localhost/public">
            Options All
            AllowOverride All
            Allow from all
        </Directory>
    </VirtualHost>
  3. Then, switch to the public folder and add a file named .htaccess to it:
    $ cd /var/www/learning-phalcon.localhost/public
    $ vim .htaccess
    
  4. Then, add the following content to the .htaccess file:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
    </IfModule>
  5. This will not work unless you have enabled mod_rewrite. To do so, execute this command:
    $ sudo a2enmod rewrite
    
  6. Now that we have configured our virtual host, let's enable it:
    $ sudo a2ensite learning-phalcon.localhost
    $ sudo service apache2 reload
    

The host file

If you open a browser and type http://www.learning-phalcon.localhost/, you'll receive a host not found or connection error. This is because there is no name resolver for this TLD (short for Top Level Domain). To fix this, we edit our host file and add this name:

$ echo "127.0.0.1 learning-phalcon.localhost www.learning-phalcon.localhost" | sudo tee /etc/hosts

Restart your browser and type the address http://www.learning-phalcon.localhost/ again. If everything goes well, you should see the current date/time.

Nginx

If you choose to use Nginx (which I recommend, especially because it can serve more concurrent clients with higher throughput, and it serves static content more efficiently) instead of Apache, here is what you need to do:

Locate the config folder of Nginx (in Ubuntu, it is installed under /etc/nginx/). Create a file named learning-phalcon.localhost in your sites-available folder (by navigating to /etc/nginx/sites-available):

$ cd /etc/nginx/sites-available
$ vim learning-phalcon.localhost

Now, add the following content to it:

server {
    listen 80;
    server_name learning-phalcon.localhost;

    index index.php;
    set $root_path "/var/www/learning-phalcon.localhost/public";
    root $root_path;

    client_max_body_size 10M;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php {
        fastcgi_index /index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_intercept_errors on;
        include fastcgi_params;

        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

Note

In some environments, you might need to edit your php.ini file and set cgi.fix_pathinfo = 0.

Then, save the file and restart Nginx:

$ sudo service nginx restart

Please edit and save your host file (check The host file section), then open your browser and type http://www.learning-phalcon.localhost/. At this point, you should see a page that shows the current date/time.

There are many possible methods to install and configure PHP and Apache/Nginx. Feel free to do a simple Google search and choose one that fits you better, if my method is not the optimal one for your needs.

Assuming that everything went well until now, we will go further by learning a little bit about Phalcon's internals.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Learning Phalcon PHP
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon