The simplest php framework. How to choose the right PHP framework. Comparative testing

However, when working as a freelancer, I often come across sites using self-written systems. Programmers do not write them out of a good life. Depending on the degree of simplicity (complexity) of the project, it is excessive or, on the contrary, insufficient, the application finished system, and reworking it takes more time than creating a website from scratch. For example, a one-page website does not require a heavy system like Joomla or a framework like Yii, but a CMS like Texpattern may not have enough functionality. Plus, the tasks set by the customer can be very specific, and quite difficult to implement on a ready-made system.

For example, you can take work with models in . It's about about ActiveRecord. Yii has an excellent guide on how to create a blog on its official website. If you stick to it and do everything as written, then after a couple of hours of studying, you can get a full-fledged blog. With categories, tags, users and admin panel.

The problems start when you try to do something that is not included in the manual. Display similar materials under the article. To do this, you need to create relational model, much more complex than presented in the examples.

As one of our comedians said, “this is where the Western begins.” What would take 3 lines of code in SQL will take a couple of nights of reading manuals and experimenting in Active Record. Because at first glance trivial task, suddenly causes an inexplicable Yii bug, which one and a half people have heard about, both Chinese. The example is not far-fetched; those who have programmed in Yii using Actve Record will support it.

This methodology for working with data is very convenient when it comes to a single table. Or even two, when the connection is one to many, over one field. The real hell breaks loose when you need to retrieve data from multiple tables.

In the world of CMS you don’t have to go far either. When working on the Yandex maps module for Joomla, it was necessary to connect it in the site admin javascript file. A week's study of the system yielded nothing. There is simply no such functionality in the modules. I must say that I got out of it using the functionality of extended fields and connected required file. But the amount of time it took me is not commensurate with if the system had been built according to my laws, and I knew what and where it was connected.

I'll talk about this in this article. How to write php framework from scratch. We will describe the basic techniques for designing MVC frameworks in pure PHP without using third-party libraries.

Frameworks are written by programmers just like you. Nothing is impossible. Write a framework for yourself on which you can then build designs for atypical sites.

I want to warn you that you should not write your bike without special need and experience. Most of the code will have to be written by hand. There will be no ready-made modules or extensions.

However, there are also advantages: the system will be completely under your control. No API restrictions or system requirements. Only pure vanillaPHP and nothing extra.

Let's leave the lyrics and finally get to the code.

Point of entry

Any website, CMS or framework starts with an entry point. This is usually index.php at the root of the site. But we won't do that. So that a programmer who works with our framework can immediately understand that all data goes through the entry point, let’s call the file main.php. Then it will be obvious - all requests are redirected through .htaccess

AdddefaultCharset UTF-8 REWRITEENGINE on Php_value Upload_Max_FileSize 50M post_value post_max_size 50m pHP_VALUE DISPLAY_RRORS 1 DirectoryinDEX Main.php? Controller = Index errordocument 404 /main.php?controller=error REWRITERULE ^index.html $ main.php rewritecond %(Request_filename)! RewriteCond %(REQUEST_FILENAME) !-d RewriteRule ^(.*)$ main.php?route=$1

The first 8 lines are auxiliary settings that will be useful for the framework in the future. We have set the default encoding to utf-8, the site will work on it. We enabled the apache Rewrite module in order to redirect all non-static requests to main.php. This is what the last 3 lines do. The entire query string that comes after the domain will be passed into the $_GET["route"] variable. Those. request http://sitename.ru/kolesa/perellli/?id=5 will turn into http://sitename.ru/main.php?route= kolesa/perelli/&id=5

Further manipulations with URL parsing are performed with using PHP. This is a common technique and you can easily use it in other systems. This is how Yii works too. But our framework, unlike Yii, cannot work without .htaccess.

The main.php file doesn't have to do much, it just defines the path constants, connects the framework and runs the application

start();

Structure

Now let's think about the structure of our framework. It is convenient for its files to be in a separate folder. Let's create a structure like this

Application -- controllers -- models -- views -- config.php - ideal -- classes -- framework.php -- config.php .htaccess main.php

our ideal =) engine will be in the ideal folder, and the user files will be in the application folder.

in the framework.php file we will define class autoloaders, so as not to write manually every time include "classname.php"; I already wrote about this technique

This file will also contain other system actions. But this will suffice for now.

Autoloaders fire when the class is used. First it is searched in the framework folder, then in the controllers folder, then in the models. Accordingly, you cannot call a controller the same as a class or module.

Now let's describe the App class, it is located in the classes folder of the engine. Its start method will launch our application.

parse();

$controller = app::gi(Router::gi()->controller."Controller");

$controller->__call("action".Router::gi()->action);

) )

The class inherits from the abstract Singleton class. The convenience is that an instance of any class that inherits from Singleton can be obtained from anywhere in the program through its gi() method. For example, an instance of our application can be obtained by App::gi(). This method will return a single instance of the App class. A single instance can create a problem when multiple databases need to be used. Therefore, it is better not to apply it to the db class.

The Router class appears in the code. It will parse the $_GET["route"] variable and return the controller and action - i.e. method of this controller. Since for now we are only writing a framework, this class will not do anything. Just fill in the appropriate fields.

controller = $_REQUEST["controller"];

if(isset($_REQUEST["action"])) $this->action = $_REQUEST["action"];

) )

The framework framework is ready. Now let's create one vital controller application/controllers/UserController.php

Users are needed in any system.He doesn't do anything yet. It just creates a User model and displays its representation on the screen. The User model inherits the classes/Model.php class

data[$name])?$this->data[$name]:null;

) function __set($name,$value)( $this->data[$name] = $value; ) )

and model code application/models/User.php

As you can see, he doesn't do anything special either.

Hello

Now it seems to you that all this is nonsense and that all this could have been made simpler. But if you think about it, our framework has enormous capabilities. It's completely structured. Expanding its capabilities will not be difficult. Adding a class for working with the database

We continue to talk about the most popular and useful tools for working with languages. This time we will talk about PHP frameworks.

Laravel

This framework has gone through a rapid journey from just promising to one of the leaders of the PHP movement. A brief description is as follows: open source, work with the MVC architectural model, convenient and intuitive interface, extended functionality.

The last point is manifested in the following possibilities:

  1. Support for third-party modules, of which there are a considerable number, which significantly expands the standard capabilities of the framework.
  2. Reverse routing, allowing you not to waste time updating links while working - everything happens automatically.
  3. Eloquent ORM design patterns that help in defining strict relationships between database objects.
  4. Automatic loading of classes. This, on the one hand, reduces the amount of code due to the absence of the need to write include..., on the other hand, unused classes are not included with all the consequences.
  5. Unit testing - having a large number of tests to prevent the accumulation of errors.
  6. Database version control system. If you expect to frequently update your product in an unimportant manner, this function will allow you not to waste time on the same type of entries.

As you understand, this is not a complete list of features that Laravel developers provide their clients. For a complete list, please visit the official website or plunge into the world of this framework in person - you will definitely like it.

CodeIgniter

This framework, which is already more than 11 years old, has gained fame due to its unpretentiousness in terms of the resources used, simplicity, convenience, a huge amount of documentation designed for developers of any level, and the absence of restrictions. At one time, Laravel was created precisely as a competitor to CodeIgniter, so until recently it was a universal reference point.

Despite its simplicity, like any popular framework, CodeIgniter also has a couple of useful features:

  1. Great support from the CodeIgniter Reactor community, including libraries, modules, templates and documentation.
  2. Templates for working with databases that are very similar to SQL syntax.
  3. Server-side caching capability.
  4. Using a package manager to quickly include libraries from the command line.

But CodeIgniter is not going to deviate from the basic idea of ​​simplicity and accessibility. Therefore, you shouldn’t expect this framework to do everything for you, although formally this is possible.

Symfony

Despite the fact that the release of the third version took place back in 2015, it is the second version of Symfony that single-handedly holds 3rd place in popularity among frameworks. The reason here is similar to CodeIgniter - speed and overall simplicity. But so that this does not conflict with functionality, the user is asked to choose one of 3 versions for specialized work:

  1. Standard Edition - for getting to know each other and performing common tasks. The Hello World Edition distribution is based on it, which contains exactly one optimization script for further use in benchmarks.
  2. Symfony CMF - adaptation for developers working with CMS systems.
  3. REST Edition - optimization for working with REST architecture (online stores, search engines, etc.).

Symfony is stereotypically considered to be a framework for command line enthusiasts. Indeed, the built-in SensioGeneratorBundle interface will help you get a whole skeleton for your code from one line of text.

An undoubted advantage will be the availability of official documentation in Russian. It is worth mentioning that it is available only for the first version of Symfony, but among the unofficial releases you will find translations of official releases and independent high-quality documentation.

Yii

Yii is touted in many rankings as Symfony's main competitor. There are indeed reasons for this: both languages ​​work with a full stack, both have source code on GitHub, both represent template development quite well. However, while Symfony provides only a model and a controller, Yii provides full MVC interaction. In addition, the interface in Yii is much more convenient, code generation using the Gii browser element is a little more powerful, and in fact, Yii will allow you to save more time on development, and the application will run a little faster.

Nette Framework

Perhaps the least known of the top PHP frameworks, which is surprising given its 13 years of age and wide capabilities. Here are some of them:

  1. One of the most productive PHP frameworks.
  2. Perfect for beginners, the learning curve is quite smooth.
  3. Powerful tools to help: Tracy - for tracking errors, Latte - a fast and intuitive template generator, Tester - a utility for high-quality testing of your application in close to real conditions.
  4. Possibility of collective work of several developers on one project.
  5. Excellent documentation and friendly community (and not only in Czech).

In general, if you haven’t tried Nette yet, we recommend it; if you find any shortcomings, be sure to write in the comments.

Short line

CakePHP is a popular Ruby on Rails clone, only focused on PHP. All the benefits are also similar.

FuelPHP is a lightweight framework that has not received due recognition due to lack of uniqueness and high expectations. As you understand, this does not affect real work for the worse.

Phpixie - one of the main features of this framework is updating. You no longer have to wait several months for a new revision. Found -> downloaded the fix -> continue working. The principle is something like this.

Fat-Free is a very lightweight, fast and simple framework for quick development. Minimum of extraneous worries.

Slim - this framework is easy to learn and get started with PHP, but is practically not in demand in the adult professional world of the web.

Phalcon is an excellent framework with high performance, negligible load on memory and file system. The downside is that the project is quite crude and has a lot of underwater C-stones.

I’ll share my thoughts and add my two cents. The article will not contain a lot of numbers or graphs (all sorts of Google Trends), only personal observations.

So, with the release of the latest versions of PHP and the emergence of new versions of popular PHP frameworks (Zend Framework 2, Yii2 (alpha), etc.), interest in the PHP language is intensifying. By the way, the language is extremely popular at the moment. Mainly among novice web developers (it is currently used on more than 80% of all websites), and among resources with average traffic.

There are, of course, examples of global websites using PHP:

Let's return to the question of PHP frameworks and choosing which one is now popular, in demand and which one should be studied. If we talk about the Western market, then the undisputed leaders in terms of demand and frequency of mention are: Zend Framework, CodeIgniter and the rapidly growing popularity of Yii. On the world's largest freelance exchanges oDesk and Elance, in addition to these three, CakePHP and Symfony are often mentioned.

In the vastness of the post-Soviet space, they are popular in descending order:

  • Zend Framework
  • CodeInginter
  • Symfony
  • Kohana
  • CakePHP

To summarize, the most popular PHP frameworks in the world according to the preferences of programmers and the requests of employers are Zend Framework, CodeIgniter And Yii. The latter is rapidly gaining popularity. Symfony and CakePHP are also common among freelance developers.

Despite the growing popularity of other scripting languages ​​(like Python and Ruby), large corporations for the most part still opt for PHP. And when choosing a platform, they are guided by such criteria as scalability, popularity of the framework and the availability of specialists in the market for this platform. In the HiLoad area, PHP is slightly inferior and has apparently reached its limit. But compiled solutions based on it appear, like kPHP, HipHop, etc.

What to study and what to focus on?

If you have basic knowledge of PHP, you would like to develop in this direction and you want your skills to be useful to an employer - you should first take a closer look at the first three frameworks: Zend, CodeIgniter, Yii. Next, you need to decide which one will be more “cute” for you and easier to learn. And finally, test them.

My personal attitude towards fireworks is the following:

— Zend Framework is popular but monstrous, there are performance problems. With knowledge of this framework, you can find a job without difficulty; another question is whether you will be able to “enter” it easily. As for me, it is difficult to study and it is not worth starting with it, IMHO.

— CodeIgniter is simple and fast. But it is very behind its competitors in terms of functionality. It’s a good place to start to understand MVC and other intricacies. But over time, you will miss the functionality out of the box.

— Yii is something in between. Slightly less productive than CodeIgniter, but contains much more functionality. There is good documentation, and in general it is much more user-friendly than Zend.

— The rest of the frameworks mentioned above are also worthy of attention, but I had no personal contact with them, and therefore I will not muddy the waters.

I started by learning CodeIgniter and fell in love with it. Then I began to lack functionality and started looking for an alternative. I am currently studying and using Yii in my work. If the question which PHP framework to choose for learning put it bluntly - then I would still be inclined to study Yii 1.1. And don’t be confused by the active work on the backwards-incompatible Yii2, it’s a long way from its release into production.

I hope I was useful to you.

Nowadays, there is no need to create your website using complex web development languages. Now you can do without creating libraries, components, model separation, and low-level security yourself. Thanks to PHP frameworks, you can skip these steps.

Let's look at the 9 best frameworks with which you can create amazing responsive websites.

1. Symfony 2

This framework is definitely not for beginners. With all its many functions of models, objects, routes, controllers, it can seem complex. But if you have a solid amount of knowledge in PHP and HTML, you can create incredible web applications with it.

Symfony is an open source project hosted on GitHub, and over 300,000 programmers have worked and improved their code with it.

Symfony 2 consists of a set of reusable PHP components that are easy to install on most platforms. And it is known to be very stable and flexible.

2. Phalcon

It is a framework written in C, the fastest PHP platform.

It offers a large number of latest features such as routing, template browsing, caching and ORM, controllers, query language, etc. Phalcon is always one step ahead of the competition thanks to low system requirements that allow you to use much less resources. And also through dependency injection, PHP, PSR-4 helper sets

autoloader and advanced routing features. It is suitable for inexperienced users as it does not take much time to learn. Here's what framework you need to choose in PHP.

3. Laravel Laravel is the most popular framework of 2016. It is also the easiest framework to learn. Its most powerful feature is its own template engine called " Blade

”, which does not consume additional resources on your site.

Laravel also has a tool for integrating third-party packages into your website. Laravel is supported by a large, active community, allowing you to get started quickly.

Another easy to use open source framework. The development process will be smooth and efficient thanks to the use of PHP5. The framework includes all the functions necessary for professional websites, as well as a CMS and CRM. These are the two starting points that form the basis of any reliable script.

When it comes to security, Yii works amazingly. Gii, available as part of Yii, is a powerful code generator. Thanks to it, you can easily create forms, modules, CRUD, models, etc.

5. CodeIgniter

A reliable, full-featured tool for creating web applications. It takes up only 2 MB of disk space, and the user manual details components that make it easy to bypass complex MVC.

CodeIgniter provides a substitution tool with templates and plugins.

6. Cake

Our ranking of PHP frameworks continues with the modern framework, which also supports 9 languages, although it was released back in 2005. CakePHP 3.3 is positioned as a powerful framework.

With its help, even novice programmers can create visually attractive websites. Framework MVC pattern, which provides model support for more efficient data management. As well as ORM functions and many components, plugins and helpers.

CakePHP is best suited for commercial sites and requires no configuration since it does not contain complex YAML or XML files.

7. ZendPHP

This framework was released 9 years ago and still remains relevant. It's an object-oriented framework, so it's best to use it for inheritance or interfaces. Its latest version is optimized for PHP7, but PHP 5.5 is also perfectly supported.

Zend has replaced the MVC stack with a simpler alternative built on middleware patterns such as Apigility.

This framework is not suitable for beginners, as it is difficult to learn. But once you get used to it, you will be able to develop large-scale web projects.

8. FuelPHP

Fuel is recommended for both beginners and professionals. This is a popular PHP framework that supports HMVC. It is recognized worldwide for its simplicity, flexibility and modern features.

Its authors prepared powerful documentation section, so that developers can create professional websites using clean syntax. The framework is characterized by advanced import capabilities, since any user can work with Fuel from any server.

9. Slim

Flexible PHP framework, which can be classified as a microframework. It comes with an optimized router, template rendering feature with custom views, secure cookies, instant messaging features, HTTP caching, error handling.

This concludes our list of the best PHP frameworks for creating responsive websites. Now you must understand that the quality of the websites created is determined not only by the level of your skill, but also by the right choice of framework.

Translation of the article " 9 Best PHP Frameworks to Build Awesome Responsive Websites» friendly project team.