search
HomeBackend DevelopmentPHP Tutorialphalcon框架使用指南,phalcon使用指南_PHP教程

phalcon框架使用指南,phalcon使用指南

初次认识phalcon是在刚学php的时候,最近终于有机会用上了.故此说说使用上的一些感受

个人是很喜欢phalcon这套框架的方式,使用依赖注入,让代码组织很灵活,耦合也很低.但是,也许是框架东西太多了,遇到几个坑. github上一年以上的issue也还没解决.不过有一定开发经验的话,还是可以自己修复的.被这几个坑浪费了不少时间....

第一个是view.

  phalcon的view,可以分成两大部分,一是全局的layout,二是controller/action的部分,
  让使用时更加方便,设置好layout后,只关注controller部分的view即可.
  但是关于view的路径设置问题,要关注这几个参数

MainView(主layout文件路径+文件名,但不包括扩展名,扩展名是在注入视图引擎时的key键值),

LayoutsDir(layouts的目录路径),

Layout(主layout文件名,可根据需求,动态使用layouts目录下的某个文件充当mainView),

PartialsDir(使用partial函数时,查找模板文件,如partial('main/header') 则查找路径是"/你的partialsDir/main/header.volt" . ),

ViewsDir(设置controller/action对应的模板文件所在的目录.和partialDir类似,自动查找目录下的模板文件)

BasePath(视图的目录的基础目录,phalcon和根据所有设置的dir来组合路径,查找模板文件,坑就是这了)

我刚开始用这个view的时候,使用的是绝对路径,因为绝对路径配置和管理比较方便.
然后我就被坑了.浪费了一个下午的时间调试.
根据官方文档给出的例子反复测试,发现这个view组件只能使用相对路径设置路径,BasePath不用设置.

一气之下就去翻phalcon的源码了.开发语言是Zephir,phalcon团队自己开发的语言.

语言语法比较简单,容易懂.然后看到了这句.

..]

看起来像是没有判断是相对路径还是绝对路径,直接组合起来了而已的,猜测是这里引发了问题.

并且是直接按顺序解析action , layout , main layout这些模板文件.

于是想验证一下.所以我写了几个调试文件,打算调试一下.

翻查phalcon源码过程省略1000字...找到必然会调用,并有路径处理的几个主要方法,继承View相关的几个class

这里也强调下,很多人喜欢用echo,一般情况是没问题,
但最好还是用文件记录吧,因为echo局限性其实挺大的,
你不知道echo buff什么时候打开,什么时候关闭..使用文件来记录log最保险了.

我测试的代码,使用多模块的方式.

BaseView文件,继承phalcon的view,并且打开这个方法的报错信息

BaseVolt文件,继承模板引擎volt

view注入代码,和目录结构如图

log记录如下

页面输出如下:

跟想像的一样,使用绝对路径时,无论如何,phalcon都是直接拼接以上设置的路径,

当开发者想动态使用controller/action的view,但layouts主目录又在上层目录时,就会出问题.

phalcon找的main layout路径是Phalcon\Mvc\View\Exception: View '/Users/Hin/work/phalcon_mode/app/modules/admin/views/layouts/main' was not found in the views directory

而我要的是/Users/Hin/work/phalcon_mode/app/views/layouts/main

然后找不到路径,main layout模板文件就不起作用了..........

<code>所以,真的就只是直接组合三个路径而已..所以,在设置路径的时候,就看自己的取舍了.</code>

phalcon view源码里三个变量中,basePath,viewDir(关键就是作者对viewDir这个变量理解有误,导致的bug) 看成是基础的路径. viewPath是依次执行_enginrender时的action view,layout view ,main view的路径.
理解了这个就可以自己慢慢设置好了路径了.....

解决办法有三个.

一 是自己写个View继承phalcon的View,处理路径. 思路是除了action的定位外,其他定位将viewDir变为空.
二 是修改phalcon源码,再自己编译一遍(在_enginRender外部处理好路径定位,去除_enginRender和路径定位的耦合)
三 是,不管了,直接使用相对路径(不设置basepath,viewsDir因为要动态定位action view,使用绝对路径, layoutsDir partialsDir使用相对路径)

github上是有人发过这个bug的issue,可是作者没理会......

------------------------------华丽分割线------------------------------------

第二是model

还有个坑,是关于phalcon的model.一样是发了issue,作者没管.
根据文档model是提供了setter 链接描述 方法的,可实际上,setter却不起作用...

<code>然后像view一样,自己修复好...</code>

个人认为setter getter是非常好用的功能,但有些人却觉得没用,见仁见智啦.
我们的开发模式是model和controller 由不同的人开发,有了setter getter,数据调用者只管拿数据就可以了,
model数据出问题,在model中修改即可,controller不需要改动.工作模式上耦合可以降低很多.

不过用这个框架的开发的项目夭折了,再深入点的坑也遇不到了.没有意外的话,短期内也不会用了.
最近心情比较糟糕.写得比较随便.当个记录好了.......

最后直接附上 github 源码,多模块模式开发用这样的目录结构也是不错的,希望能帮到别人

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1102460.htmlTechArticlephalcon框架使用指南,phalcon使用指南 初次认识phalcon是在刚学php的时候,最近终于有机会用上了.故此说说使用上的一些感受 个人是很喜欢ph...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.