Phalcon Framework Usage Guide, Phalcon usage Guide _php tutorial

Source: Internet
Author: User

Phalcon Framework Usage Guide, Phalcon usage guide

When I first met Phalcon, I had a chance to use it recently when I first learned PHP. So say something about how it feels

The individual is very fond of phalcon this framework, using dependency injection, so that the code organization is flexible, and the coupling is very low. But maybe the frame is too much to meet a few pits. More than a year on GitHub, issue has not been resolved. But there is a certain development experience, it can be repaired by themselves. Wasted some time by these pits ....

The first one is view.

  Phalcon view, can be divided into two parts, one is the overall layout, the second is the controller/action part,  let the use of more convenient, set layout, only focus on the controller part of the view.  But with regard to the path setting of the view, here are a few parameters to follow

MainView (main layout file path + file name, but not extension, extension is the key key value when injecting the view engine),

Layoutsdir (directory path for layouts),

Layout (main layout file name, can dynamically use a file under the Layouts Directory to act as Mainview, as required),

Partialsdir (when using the partial function, find the template file, such as partial (' Main/header '), the lookup path is "/Your Partialsdir/main/header.volt".),

Viewsdir (sets the directory where the template file for Controller/action is located. Similar to Partialdir, automatically finds template files in the directory)

BasePath (the base directory of the View directory, the Phalcon and the dir of all the settings to assemble the path, find the template file, the pit is this)

When I first started using this view, I used the absolute path, because the absolute path configuration and management is more convenient.
Then I was in the pit. Wasted an afternoon of time debugging.
Based on the examples given in the official documentation, it is found that the view component can only use relative paths to set the path, BasePath not set.

In a fury to go to the Phalcon source. The language of development is the language developed by the Zephir,phalcon team.

Language grammar is relatively simple and easy to understand. And then I saw this sentence.


It seems that there is no judgment is the relative path or absolute path, the direct combination of just, guess is here caused the problem.

and is directly in order to parse the action, layout, main layout these template files.

So I want to verify. So I wrote a few debug files, and I'm going to debug it.

Search Phalcon source code process omitted 1000 words ... Find some of the main methods that are bound to be called and have path handling, inheriting view-related several class

Here also stressed that a lot of people like to use Echo, the general situation is no problem, but it is best to use the document record it, because the echo limitation is actually quite large, you do not know when the echo buff Open, when closed. Using a file to record log is the safest.

I'm testing the code, using a multi-module approach.

Baseview file, inherit the Phalcon view, and open the error message for this method

Basevolt file, inheriting template engine volt

View injection code, and directory structure

Log records are as follows

The page output is as follows:

As you can imagine, when using absolute paths, Phalcon are directly stitching the paths above the settings,

When developers want to use Controller/action view dynamically, but the layouts home directory is in the upper directory, there will be problems.

Phalcon find the main layout path isPhalcon\Mvc\View\Exception: View '/Users/Hin/work/phalcon_mode/app/modules/admin/views/layouts/main' was not found in the views directory

And I want it./Users/Hin/work/phalcon_mode/app/views/layouts/main

Then the path cannot be found, and the main layout template file will not work ....


Phalcon View source Three variables, Basepath,viewdir (the key is the author of viewdir this variable understanding error, caused by the bug) as the basis of the path. Viewpath is the path of the action View,layout view, main view, which executes _enginrender in turn.
Understand this you can slowly set up the path .....

There are three solutions. One is to write a view that inherits Phalcon's view, processing the path. The idea is that in addition to the action's positioning, other positioning will viewdir to null. The second is to modify the Phalcon source code, and then compile their own (in the _enginrender external processing path positioning, removal of _enginrender and path positioning coupling) Three is, regardless of, Use relative path directly (do not set Basepath,viewsdir because you want to dynamically locate the action view, use an absolute path, layoutsdir partialsdir use a relative path) GitHub is the issue that someone has sent this bug, But the author ignored ...

------------------------------Ornate Split Line------------------------------------

The second is model

There is also a hole, is about Phalcon model. The same is the issue, the author did not control.
According to the document model is to provide a setter link description method, but in fact, the setter does not work ...


Personally, the setter getter is a very useful function, but some people feel useless, a matter of opinion. Our development model is the model and controller developed by different people, with setter getter, the data caller can just take the data, Model data problems, in the model can be modified, controller does not need to change. The coupling on the operating mode can be reduced a lot.

However, the development of the framework of the project was aborted, and then the depth of the pit is not met. No accident, not in the short term.
I've been in a bad mood lately. Be a good writer.

Finally attached GitHub source code, multi-module mode development with such a directory structure is also good, hope to help others true techarticle Phalcon Framework Usage Guide, Phalcon User Guide The first time I knew Phalcon was in the early days of PHP, I finally got a chance to use it. So say the use of some of the feelings of the individual is very like ph ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.