Symfony2 是什么(转)

本文转自:http://www.cnblogs.com/Seekr/archive/2012/06/15/2550894.htmlphp

Symfoy2 是什么?html

PHP世界里又一广受关注的web MVC框架? Fabien Potencier 却不这么说!web

Fabien Potencier这样定义Symfoy2 是个什么东西: api

  首先,Symfony2 是一个独立,松散的,有组织严密的PHP组件集合,它能够为你解决一些web开发中遇到的通常性问题。浏览器

  其次,基于这些组件,Symfoy2 也能够做为一个独立的web框架使用。缓存

那么Symfony2 是一个MVC框架吗?服务器

Fabien Potencier 说Symfony2历来没有把本身定义为一个MVC框架!app

那它是什么? Fabien Potencier 咱们历来不关心MVC模式,关心的只有各个关注点的分离(separation of concerns)。框架

可是Symfony2 仍是提供了部分的MVC模式的实现:好比Controller部分,View部分却没有Mode部分不过你能够经过和它紧密继承的ORM(Doctrine2和Propel)实现。分布式

 

从这个角度看Symfony的确也没有逃出web MVC框架的圈子啊!!!

Fabien Potencier 又说Symfony2历来就没有想靠这些ORM来使本身成为另外一个MVC的追随者,咱们的目标更远大!

 

告诉你吧, Symfony2 是一个HTTP框架或者说是一个Request/Response 框架。咱们紧盯的目标不是MVC模式,而是HTTP协议,咱们是更低级的更基础的框架。

咱们为何要这么说呢? 有根据的!

近几年随着web的发展,有时候你只须要建立一组REST API,全部的逻辑都放到浏览器端,服务器端只提供数据就是一种web了。不信你看 backbone.js !

再说了,MVC模式只不过是Web 应用程序的其中一种实现方式罢了。

剥去全部框架模式的皮,你看看那个web程序不是处理一个接收到的Request而后返回一个Response啊?

咱们Symfony2 抓住的就是web程序的根本! 再说咱们众多的HTTP流媒体有哪一个会选择使用MVC呢?

总之,咱们Symfony2比MVC更靠近根本,咱们更底层,更通用!!!

 

提及Symfony2,Fabien Potencier说咱们有着更加远大的目标,怎么解释呢?

Symfony2 将继续专一于Pack技术的研究和创新!咱们相信她会继续推进web的向前发展。

先看看Symfony2 中咱们已经包含的创新吧!

从Bundles,HTTP 缓存,分布式,依赖注入,模板引擎,声明式配置,资产管理,稳定的API到web分析器等等一系列技术都对web的发展起到了巨大的推进做用。

 

“ 要知道一个独立的框架永远不可能成为PHP世界里的一个标准,因此Symfony2 在探寻另一条路!”

“ 共享无处不在。”

“ 咱们不能重复制造轮子。”

所以,咱们紧密的集成了Monolog,Composer,Doctrine,Propel,Assetic,Twig,Swiftmailer等伟大产品。

更重要的是咱们想跟你们分享咱们的工做!

因此,咱们最终选择了走组件(components)化这条路!

咱们将为一切web项目提供建筑模块,不管是我的项目仍是商业项目,更或者是开源项目!

 

听说在Symfony2 的代码中可能会有标志为@api的类或者方法,它意味着一个方法从名字到参数以及返回值都不会由于Symfony2发展版本而变化,因此,若是

你的项目只使用了这些,那么你就不用担忧Symfony2的版本升级问题。

 

看看Symfony2 如今拥有的组件吧:

DependencyInjection
EventDispatcher
HttpFoundation
DomCrawler
ClassLoader
CssSelector
HttpKernel
BrowserKit
Templating
Translation
Serializer
Validator
Security
Routing
Console
Process
Config
Finder
Locale
Yaml
Form

Fabien 简单介绍了几个bundle:

1. ClassLoader:

  实现了PSR-o 标准(自动加载具备命名空间的类,适用于PHP5.3以上)的自动加载器,同时它也能按照PEAR命名规则加载类。它很是灵活能够基于子命名空间在不一样的目录中查询要加载的类。你甚至能够为一个命名空间指定多个目录。

 1 require_once __DIR__.'/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
 2  
 3 use Symfony\Component\ClassLoader\UniversalClassLoader;
 4  
 5 $loader = new UniversalClassLoader();
 6 $loader->registerNamespaces(array(
 7     'Symfony'          => array(__DIR__.'/src', __DIR__.'/symfony/src'),
 8     'Doctrine\\Common' => __DIR__.'/vendor/doctrine-common/lib',
 9     'Doctrine\\DBAL'   => __DIR__.'/vendor/doctrine-dbal/lib',
10     'Doctrine'         => __DIR__.'/vendor/doctrine/lib',
11     'Monolog'          => __DIR__.'/vendor/monolog/src',
12 ));
13 $loader->registerPrefixes(array(
14     'Twig_' => __DIR__.'/vendor/twig/lib',
15 ));
16 $loader->register();

若是你想获取更加高的执行效率,能够选择使用APC缓存版Universal类加载器。

 

2.Console 命令行工具

在建立web应用程序时使用命令行工具很方便,你能够想以下代码同样建立本身的命令行工具:

 1 use Symfony\Component\Console\Application;
 2 use Symfony\Component\Console\Input\InputInterface;
 3 use Symfony\Component\Console\Input\InputArgument;
 4 use Symfony\Component\Console\Input\InputOption;
 5 use Symfony\Component\Console\Output\OutputInterface;
 6  
 7 $console = new Application();
 8 $console
 9     ->register('ls')
10     ->setDefinition(array(
11         new InputArgument('dir', InputArgument::REQUIRED, 'Directory name'),
12     ))
13     ->setDescription('Displays the files in the given directory')
14     ->setCode(function (InputInterface $input, OutputInterface $output) {
15         $dir = $input->getArgument('dir');
16  
17         $output->writeln(sprintf('Dir listing for <info>%s</info>', $dir));
18     })
19 ;
20 $console->run();

3.YAML  一种如今很流行的配置格式。

use Symfony\Component\Yaml\Yaml;
 
$array = Yaml::parse($file);
 
print Yaml::dump($array);

4. Finder 优秀文件资源的操做接口。

 1 use Symfony\Component\Finder\Finder;
 2  
 3 $finder = new Finder();
 4  
 5 $iterator = $finder
 6   ->files()
 7   ->name('*.php')
 8   ->depth(0)
 9   ->size('>= 1K')
10   ->in(__DIR__);
11  
12 foreach ($iterator as $file) {
13     print $file->getRealpath()."\n";
14 }

你甚至能够用它获取远程服务器文件系统中的资源,好比获取Amazon S3上的文件:

1 $s3 = new \Zend_Service_Amazon_S3($key, $secret);
2 $s3->registerStreamWrapper("s3");
3  
4 $finder = new Finder();
5 $finder->name('photos*')->size('< 100K')->date('since 1 hour ago');
6 foreach ($finder->in('s3://bucket-name') as $file) {
7     print $file->getFilename()."\n";
8 }

5.Process 进程组件,你能够用来在一个外部进程中执行命令!下面例子是执行一个简单的目录列表命令并返回结果:

 1 use Symfony\Component\Process\Process;
 2  
 3 $process = new Process('ls -lsa');
 4 $process->setTimeout(3600);
 5 $process->run();
 6 if (!$process->isSuccessful()) {
 7     throw new RuntimeException($process->getErrorOutput());
 8 }
 9  
10 print $process->getOutput();

若是你想监控执行过程,你能够给run方法传入一个匿名方法:

 1 use Symfony\Component\Process\Process;
 2  
 3 $process = new Process('ls -lsa');
 4 $process->run(function ($type, $buffer) {
 5     if ('err' === $type) {
 6         echo 'ERR > '.$buffer;
 7     } else {
 8         echo 'OUT > '.$buffer;
 9     }
10 });

6.DomCrawler jQuery的php版本!你能够用它导航定位HTML的DOM结构或者XML文档。

1 use Symfony\Component\DomCrawler\Crawler;
2  
3 $crawler = new Crawler();
4 $crawler->addContent('<html><body><p>Hello World!</p></body></html>');
5  
6 print $crawler->filterXPath('descendant-or-self::body/p')->text();

7.CssSelector 咱们常常用XPath来访问Dom结构,其实用Css 选择器更加容易,这个组件就是把Css选择器转为XPath等效的东西。

1 use Symfony\Component\CssSelector\CssSelector;
2  
3 print CssSelector::toXPath('div.item > h4 > a');

因此你可使用CssSelector 和DomCrawler来替代XPath:

1 use Symfony\Component\DomCrawler\Crawler;
2  
3 $crawler = new Crawler();
4 $crawler->addContent('<html><body><p>Hello World!</p></body></html>');
5  
6 print $crawler->filter('body > p')->text();

8.HttpFoundation 

该组件只是在PHP的相关web内容上面增长了一个面向对象层,包括Request,Response,Uploaded files,Cookies,Sessions...

1 use Symfony\Component\HttpFoundation\Request;
2 use Symfony\Component\HttpFoundation\Response;
3  
4 $request = Request::createFromGlobals();
5 echo $request->getPathInfo();

你用它能够很容易的建立本身的Request 和 Response:

1 $request = Request::create('/?foo=bar', 'GET');
2 echo $request->getPathInfo();
3 
4 
5 $response = new Response('Not Found', 404, array('Content-Type' => 'text/plain'));
6 $response->send();

9.Routing 

路由组件和Request对象是相互配合着把Request转换为Response。

 1 use Symfony\Component\HttpFoundation\Request;
 2 use Symfony\Component\Routing\Matcher\UrlMatcher;
 3 use Symfony\Component\Routing\RequestContext;
 4 use Symfony\Component\Routing\RouteCollection;
 5 use Symfony\Component\Routing\Route;
 6  
 7 $routes = new RouteCollection();
 8 $routes->add('hello', new Route('/hello', array('controller' => 'foo')));
 9  
10 $context = new RequestContext();
11  
12 // this is optional and can be done without a Request instance
13 $context->fromRequest(Request::createFromGlobals());
14  
15 $matcher = new UrlMatcher($routes, $context);
16  
17 $parameters = $matcher->match('/hello');

10.EventDispatcher

 1 use Symfony\Component\EventDispatcher\EventDispatcher;
 2 use Symfony\Component\EventDispatcher\Event;
 3  
 4 $dispatcher = new EventDispatcher();
 5  
 6 $dispatcher->addListener('event_name', function (Event $event) {
 7     // ...
 8 });
 9  
10 $dispatcher->dispatch('event_name');

11.DependencyInjection

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
 
$sc = new ContainerBuilder();
$sc
    ->register('foo', '%foo.class%')
    ->addArgument(new Reference('bar'))
;
$sc->setParameter('foo.class', 'Foo');
 
$sc->get('foo');

12.HttpKernel

Http 内核组件提供了HTTP协议中最有活力的部分,如下面接口的形式定义展现,它也是Symfony2框架的核心。

 1 interface HttpKernelInterface
 2 {
 3     /**
 4      * Handles a Request to convert it to a Response.
 5      *
 6      * @param  Request $request A Request instance
 7      *
 8      * @return Response A Response instance
 9      */
10     function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true);
11 }

它接受一个Request输入并返回一个Response输出。 只要遵循这个接口规定,你就能使用Symfony2中全部的精彩内容。

 

下面使用Symfony2 组件来建立一个简单的框架:

 1 $routes = new RouteCollection();
 2 $routes->add('hello', new Route('/hello', array('_controller' =>
 3     function (Request $request) {
 4         return new Response(sprintf("Hello %s", $request->get('name')));
 5     }
 6 )));
 7  
 8 $request = Request::createFromGlobals();
 9  
10 $context = new RequestContext();
11 $context->fromRequest($request);
12  
13 $matcher = new UrlMatcher($routes, $context);
14  
15 $dispatcher = new EventDispatcher();
16 $dispatcher->addSubscriber(new RouterListener($matcher));
17  
18 $resolver = new ControllerResolver();
19  
20 $kernel = new HttpKernel($dispatcher, $resolver);
21  
22 $kernel->handle($request)->send();

ok, 这就是框架了!

若是想添加一个HTTP反向代理以获取HTTP caching和ESI(Edge Side Includes)带来的好处,那么这样作!

1 $kernel = new HttpKernel($dispatcher, $resolver); 
2  
3 $kernel = new HttpCache($kernel, new Store(__DIR__.'/cache'));

想对它作一下功能测试:

1 $client = new Client($kernel);
2 $crawler = $client->request('GET', '/hello/Fabien');
3  
4 $this->assertEquals('Fabien', $crawler->filter('p > span')->text());

想要一个好看的错误展现页面?

1 $dispatcher->addSubscriber(new ExceptionListener(function (Request $request) {
2     $msg = 'Something went wrong! ('.$request->get('exception')->getMessage().')';
3  
4     return new Response($msg, 500);
5 }));
相关文章
相关标签/搜索