Phalcon framework User Guide _ php instances

Source: Internet
Author: User
Phalcon is a framework of php processes outside China, but the Phalcon framework in China is not very good. I will introduce you to the phalcon framework tutorial today, I hope that the following will help you get to know phalcon for the first time. When I first learned php, I finally got a chance to use it. so let's talk about some usage feelings.

I personally like the phalcon framework. Using dependency injection makes code organization flexible and coupling very low. however, it may be that there are too many frameworks and there are several pitfalls. github's issue for more than a year has not been resolved yet. however, if you have some development experience, you can fix it yourself. these pitfalls have wasted a lot of time ....

The first is view.

The view of phalcon can be divided into two parts: one is global layout and the other is the controller/action part, which makes it easier to use. After setting layout, only focus on the view of the controller. however, you need to pay attention to these parameters when setting the view path.

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

LayoutsDir (directory path of layouts ),

Layout (name of the main layout file, which dynamically uses a file in the layouts directory as the mainView as needed ),

PartialsDir (when using the partial function, find the template file, for example, partial ('main/head'). The search path is "/Your partialsDir/main/header. volt ".),

ViewsDir (set the directory where the template file corresponding to the controller/action is located. Similar to partialDir, the template file under the directory is automatically searched)


When I first started using this view, I used an absolute path because it is convenient to configure and manage absolute paths.
Then I got stuck. I wasted an afternoon debugging.
According to the example given in the official document, we found that this view component can only use relative paths to set the path, BasePath does not need to be set.

The source code of phalcon will be turned over in one breath. The development language is Zephir, and the language developed by the phalcon team.

The language syntax is relatively simple and easy to understand. Then you can see this sentence.


It seems that the relative path or absolute path has not been determined and is directly combined. I guess it is a problem here.

In addition, the template files such as action, layout, and main layout are directly parsed in order.

So I want to verify it. So I wrote a few debugging files and planned to debug them.

The phalcon source code process is omitted by 1000 words... find the main methods that will be called, and there are several main methods for path processing, inherit the class related to View

It is also emphasized that many people like to use echo. Generally, it is okay, but it is best to use file records, because echo is quite limited, you don't know when echo buff will be opened or when it will be closed .. using files to record logs is the safest.

The code I tested uses multiple modules.

The BaseView file inherits the view of phalcon and returns an error message when this method is enabled.

BaseVolt file, inheriting template engine volt

View injection code and directory structure

The log records are as follows:

The page output is as follows:

As you can imagine, when using absolute paths, phalcon directly Concatenates the preceding paths,

When developers want to dynamically use the controller/action view, but the layouts main directory is in the upper directory, a problem occurs.

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

What I want is/Users/Hin/work/phalcon_mode/app/views/layouts/main

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

Therefore, it is really just a combination of three paths. Therefore, when setting the path, you can choose between them.

Among the three variables in the phalcon view source code, basePath and viewDir (the key is the author's incorrect understanding of the viewDir variable, resulting in Bugs) are considered as the basic path. viewPath is the action view, layout view, and main view path when _ enginrender is executed in sequence.
After understanding this, you can set the path by yourself .....

There are three solutions. first, write a View to inherit the phalcon View and process the path. in addition to action positioning, other positioning methods change viewDir to null. the second is to modify the phalcon source code and compile it again. (The path location is handled externally in _ enginRender, and the coupling between _ enginRender and path location is removed, directly use the relative path (basepath is not set, because viewsDir needs to dynamically locate the action view and use the absolute path, layoutsDir partialsDir uses the relative path). Someone posted this bug on github, but the author ignores it ......

------------------------------ Gorgeous split line ------------------------------------

The second is model.

There is also a pitfall, which is about the phalcon model. The same is the issue of issue, and the author does not care about it.
According to the document model, the setter link description method is provided. In fact, setter does not work...

And fix it yourself like view...

I personally think that setter getter is a very useful function, but some people think it is useless. our development mode is that model and controller are developed by different people. With setter getter, data callers only need to take the data. If the model data has a problem, modify it in the model, the controller does not need to be modified. coupling can be greatly reduced in the working mode.

However, the development project using this framework is aborted, and the deep-seated pitfalls cannot be met. If there is no accident, it will not be used in the short term.
Recently, I am in a bad mood. I wrote it casually. I have recorded it .......

The github source code is directly attached. This directory structure is also good for multi-module development, hoping to help others

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.