Phalcon 0.9.0 BETA版本发布,新增大量功能

很高兴,Phalcon 0.9.0 BETA版本发布了,这个版本主要是引入开发社区要求的一些新功能。感谢他们,曾参与提供意见以及帮助测试。

通过魔术属性获取以及保存数据

现在通过ORM可以更容易的获得相关Model的数据,只需要简单的通过访问属性关系别名即可(前提是必须在Model中建立关系模型):

<?php

// Get a song
$song = Songs::findFirst(100);

// Get the album name
// Note we are accessing the Album relationship of the song 
echo $song->album->name;

// Find an album 
$album = Albums::findFirst(70);

// Print all songs related to an album
foreach ($album->songs as $song) {
	echo $song->name;	
}

// Delete all the songs related to the album
$album->songs->delete();

魔法属性同时也可以用于保存Model对象以及关联关系:

<?php

// Create an artist
$artist = new Artists();
$artist->name = 'Shinichi Osawa';
$artist->country = 'Japan';

// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist; //Assign the artist
$album->year = 2008;

// Save the album and the artist at the same time
// This saves as a transaction so if anything goes wrong with 
// saving the related records, the parent will not saved either
// Messages are passed back to the user for information regarding
// any errors
$album->save();

同时,也支持has-many关联关系:

<?php

// Get an existing artist
$artist = Artists::findFirst('name = "Röyksopp"');

// Get an album
$album = new Albums();
$album->name = 'Junior';
$album->artist = $artist;

$songs = array();

// Create a first song
$songs[0] = new Songs();
$songs[0]->name = 'Happy up Here';
$songs[0]->duration = '2:44';

// Create a second song
$songs[1] = new Songs();
$songs[1]->name = 'The Girl and the Robot';
$songs[1]->duration = '4:29';

// Assign the songs array
$album->songs = $songs;

// Save the album + its songs
$album->save();

事件组件的优先级

事件组件目前正式支持优先级,有了这个功能,你可以设定监听器的侦听优先级。

<?php

$evManager->attach('db', new DbListener(), 150); //More priority
$evManager->attach('db', new DbListener(), 100); //Normal priority
$evManager->attach('db', new DbListener(), 50); //Less priority

Annotations

我们实现Annotations是由于社区的要求。这是Phalcon的另一个第一次,也是PHP世界第一次用C语言实现了Annotations解析组件。Phalcon\Annotations是一个通用组件,在项目中,可以方便解析及添加缓存注释。

假设开发人员要为下面的控制器创建一个插件,如果Action被标记,则自动启用缓存。

<?php
 
class NewsController extends \Phalcon\Mvc\Controller
{
 
    public function indexAction()
    {
 
    }
 
    /**
     * @Cache(lifetime=86400)
     */
    public function showAction($slug)
    {
        $this->view->article = Article::findFirstByTitle($slug);
    }
 
}

我们创建分发器服务使其发送事件到事件管理器:

<?php

$eventsManager = new \Phalcon\Events\Manager();
 
//Attach the plugin to 'dispatch' events
$eventsManager->attach('dispatch', new CacheEnablerPlugin());
 
// Setup the dispatcher service to send events to an events manager
$di->set('dispatcher', function() use ($eventsManager) {
	$dispatcher = new \Phalcon\Mvc\Dispatcher();
	$dispatcher->setEventsManager($eventsManager);
	return $dispatcher;
});

CacheEnablerPlugin 将开启视图缓存。

<?php
 
class CacheEnablerPlugin extends \Phalcon\Mvc\User\Plugin
{
 
    public function beforeExecuteRoute($event, $dispatcher)
    {
 
        //Get the method annotations
        $annotations = $this->annotations->getMethod(
            $dispatcher->getActiveController(),
            $dispatcher->getActiveMethod()
        );
 
        //Check if the method had implemented an annotation 'Cache'
        if ($annotations->has('Cache')) {
 
            //Get the lifetime parameter
            $lifetime = $annotations->get('Cache')->getNamedParameter('lifetime');
 
            //Enable the cache
            $this->view->cache(array('lifetime' => $lifetime));
        }
 
    }
 
}

完整的示例请看这里

Phalcon\Annotations 允许在不同的组件实现 annotations (并不仅仅是视图)

基于注释的路由器

利用新的注释功能,我们将要为你介绍一个变种的基于注释的路由器,在控制器文件中通过注释实现路由器功能,这使得更方便的管理多个项目或更复杂的路由功能,因为你的路由表直接被附加到各自的控制器文件中,同时也增加了代码的可读性。

下面是通过注释实现路由功能的示例:

<?php

namespace MyApi\Controllers\Users;
 
/**
 * @RoutePrefix("/robots")
 */
class UsersController extends Phalcon\Mvc\Controller
{
 
	/**
	 * @Get("/")
	 */
	public function indexAction()
	{
 		//...
	}
 
	/**
	 * @Get("/find/{id:[0-9]+}", name="find-user")
	 */
	public function findAction($id)
	{
 		//...
	}
 
	/**
	 * @Route("/save", methods={"POST", "PUT"}, name="save-user")
	 */
	public function saveAction()
	{
 		//...
	}

	/**
	 * @Delete("/delete/{id:[0-9]+}")
	 */
	public function removeAction($id)
	{
 		//...
	}
 
}

通过注释实现的路由必须这样使用:

<?php

$di->set('router', function() {

	//Use the annotations router
	$router = new \Phalcon\Mvc\Router\Annotations(false);
			 
	//Read the annotations in MyApi\Controllers\Users if the uri starts with /api/users
	$router->addResource('Api\Controllers\Users', '/api/users');

	//Read the annotations in MySite\Controllers\Blog if the uri starts with /blog
	$router->addResource('Web\Controllers\Blog', '/blog');
	 
	return $router;
});

该版本除了修正了一些bugs,还添加了一些小的改进。你可以在这里查看更完整的修改日志

帮助测试

安装此版本请从0.9.0分支安装

git clone http://github.com/phalcon/cphalcon
cd build
git checkout 0.9.0
sudo ./install

Windows用户可以通过下载页面直接下载 DLL 扩展文件

所有的测试都通过Travis测试过,所以该版本不应该有重大的BUG。请帮助我们测试并报告BUG。如果你有任何疑问,可随时通过Stack OverflowGoogle Group提问。

中文文档: http://phalcon.5iunix.net

英文文档: http://phalconphp.com/documentation

原文地址:https://www.cnblogs.com/shihao/p/2871819.html