ホームページ >バックエンド開発 >PHPチュートリアル >symfony2 安装并创建第一个页面,symfony2第一个_PHP教程
1、安装和配置
参考 http://symfony.cn/docs/book/installation.html
使用安装工具:
windows系统
Open your command console and execute the following command:
c:<span class="se">\> php -r <span class="s2">"readfile('http://symfony.com/installer');" > symfony.phar </span></span>
Then, move the downloaded <span class="pre">symfony.phar</span>
file to your projects directory and execute it as follows:
c:<span class="se">\> move symfony.phar c:<span class="se">\projects c:<span class="se">\projects<span class="se">\> php symfony.phar</span></span></span></span>
创建symfony应用:
Once the Symfony Installer is ready, create your first Symfony application with the <span class="pre">new</span>
command:
<span class="c"># Linux, Mac OS X <span class="nv">$ symfony new my_project_name <span class="c"># Windows c:<span class="se">\> <span class="nb">cd projects/ c:<span class="se">\projects<span class="se">\> php symfony.phar new my_project_name </span></span></span></span></span></span></span>
运行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:
<span class="nv">$ <span class="nb">cd my_project_name/ <span class="nv">$ php app/console server:run </span></span></span>
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
<?<span>php namespace Test\WebBundle\Controller; </span><span>use</span><span> Symfony\Bundle\FrameworkBundle\Controller\Controller; </span><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; </span><span>use</span><span> Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; </span><span>class</span> DefaultController <span>extends</span><span> Controller { </span><span>/*</span><span>* * @Route("/hi/{name}") * @Template() </span><span>*/</span> <span>//</span><span>以上注释并不是没有用,是利用注释动态影响程序代码.<span>@Template()</span>使用默认视图文件</span> <span>public</span> <span>function</span> indexAction(<span>$name</span><span>) {<br /> <span>//$name的值为</span><span>路由{}中name的值</span> </span><span>return</span> <span>array</span>('name' => <span>$name</span><span>);//返回name的值给视图文件 } }</span>
Default/index.html.twig
Hello {{ name }}!
浏览器中输入http://localhost:8000/app_dev.php/hi/world,页面中可以打印出Hello world!