本文轉自: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 }));