symfony2 安装并创建第一个页面

1、安装和配置

参考 http://symfony.cn/docs/book/installation.html

使用安装工具:

windows系统

Open your command console and execute the following command:

c:\> php -r "readfile('http://symfony.com/installer');" > symfony.phar 

Then, move the downloaded symfony.phar file to your projects directory and execute it as follows:

c:\> move symfony.phar c:\projects c:\projects\> php symfony.phar

创建symfony应用:

Once the Symfony Installer is ready, create your first Symfony application with the new command:

# Linux, Mac OS X
$ symfony new my_project_name # Windows c:\> cd projects/ c:\projects\> php symfony.phar new my_project_name 

运行symfony应用:

Symfony leverages the internal web server provided by PHP to run applications while developing them. Therefore, running a Symfony application is a matter of browsing the project directory and executing this command:

$ cd my_project_name/ $ php app/console server:run 

2、目录

/app:存在缓存、配置文件、日志及核心配置参数;

/bin:存放用到的执行文件;

/src:自己编写的源代码;视图文件放在view文件夹下

/vendor:存放第三方代码;

/web/app.php:单一入口文件

检查配置:

命令行 d:\symfony2.3\app>php check.php

或 浏览器输入http://localhost:8000/config.php

3、编写一个hello world页面

>php app/console generate:bundle   创建一个新的bundle

Controller/DefaultController.php

<?php

namespace Test\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    /**
     * @Route("/hi/{name}")
     * @Template()
     */
    //以上注释并不是没有用,是利用注释动态影响程序代码.@Template()使用默认视图文件
    public function indexAction($name)
    {
     //$name的值为路由{}中name的值
return array('name' => $name);//返回name的值给视图文件 } }

Default/index.html.twig

Hello {{ name }}!

浏览器中输入http://localhost:8000/app_dev.php/hi/world,页面中可以打印出Hello world!

 

你可能感兴趣的:(symfony2 安装并创建第一个页面)