Welcome! If you’re here, it’s likely because you’re excited about harnessing the power of Phalcon, the high-performance PHP framework, in a Linux environment. In this article, we’ll walk you through the process of setting up your first project using Phalcon Developer Tools on a Linux system.

Advertisement

Prerequisites

Before starting, ensure that you have the following installed on your system:

  1. PHP 5.7 or higher: Phalcon is a PHP-based framework, so you’ll need a compatible PHP version installed. You can check your PHP version by running `php -v` in your terminal.
  2. Phalcon PHP extension: This is the core Phalcon framework. You can find detailed installation instructions on the Phalcon installation page.
  3. Phalcon Developer Tools: This provides handy scripts for creating and managing your projects. Installation instructions can be found on the Phalcon DevTools Github page.
  4. MySQL or PostgreSQL: Phalcon supports many database systems, but for this tutorial, we’ll use either MySQL or PostgreSQL.

Step 1: Installing Phalcon Developer Tools

The Phalcon Developer Tools provide an array of useful command-line scripts for creating projects, controllers, models, and migrations. They can be installed globally or locally within a project. For the sake of this guide, we’ll install them globally.

Open your terminal and execute the following commands:

cd ~ 
git clone git://github.com/phalcon/phalcon-devtools.git 
cd ~/phalcon-devtools/ 
sudo ./phalcon.sh 
echo 'export PATH=$PATH:~/phalcon-devtools/' >> ~/.bashrc 
source ~/.bashrc 

After successfully running these commands, you should be able to access the phalcon command from your terminal. You can confirm the installation by running phalcon. You should see a list of commands available to you.

Step 2: Creating Your First Phalcon Project

Now that we have everything set up, let’s create a new project. Phalcon DevTools includes a project bootstrap script that will generate a directory structure for your application. To create a new project, run:

phalcon create-project my_first_project 

This will create a new directory `my_first_project` in your current directory.

Your basic project structure will look like this:

Step 3: Configuring the Project

Next, you need to configure your new project to work with your database. Navigate to `app/config` and open `config.php`. Here you need to update your database credentials:

Step 4: Creating a Controller

Phalcon DevTools makes it simple to create controllers. For instance, to create a `UserController`, you can run:

cd my_first_project 
phalcon create-controller user 

This will generate a `UserController` in `app/controllers/` with an indexAction method.

Step 5: Creating a Model

Creating a model is just as straightforward. Let’s create a User model:

phalcon create-model user 

This will create a User model in `app/models/`. By default, it assumes that you have a user table in your database.

Step 6: Running the Project

Finally, to run your project, you’ll need to start the PHP built-in server. In the `public` directory of your project, run:

php -S localhost:8000 

Then navigate to http://localhost:8000 in your browser. You should see your new Phalcon project running!

Conclusion

Congratulations! You’ve just created your first Phalcon project on Linux using the Phalcon Developer Tools. This is just the beginning of your journey. The Phalcon framework and its Developer Tools provide a robust and efficient environment for developing PHP applications.

While this guide has provided you with the basics to get started, remember that Phalcon is a very powerful and flexible framework. Be sure to delve deeper into the documentation to unlock its full potential. Happy coding!

Share.

3 Comments

  1. chmod +x /usr/local/bin/phalcon
    chmod: cannot operate on dangling symlink ‘/usr/local/bin/phalcon’
    ubuntu@ip-172-31-14-255:~$ phalcon commands

    • After the “`composer install“` command run this command before using “`ls -s command“`:
      “`
      sudo mv /opt/phalcon/vendor/phalcon/devtools/phalcon /opt/phalcon/vendor/phalcon/devtools/phalcon.php
      “`
      for rename file phalcon to phalcon.php. Because, I don’t know why, but in the git they downloaded a phalcon file with the name just phalcon, not phalcon.php. So we need to rename this file.

Exit mobile version