Symfony is a PHP full-stack web framework. It is written with speed and flexibility in mind. It allows developers to build better and easy to maintain websites with PHP.
优点:
1. 更加注重业务逻辑,减少重复造轮子。
2. 快速开发简装、可持续升级的网站应用。
3. 更深入理解并应用网站开发领域中的最佳实践。
# 安装
mkdir -p /usr/local/bin | |
curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony | |
chmod a+x /usr/local/bin/symfony |
创建新工程
symfony new my_project_name |
PHP CLI-Server 搭建简易开发环境
bin/console server:run -vvv |
安装 composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
php -r "if (hash_file('SHA384', 'composer-setup.php') === '61069fe8c6436a4468d0371454cf38a812e451a14ab1691543f25a9627b97ff96d8753d92a00654c21e2212a5ae1ff36') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" | |
php composer-setup.php | |
php -r "unlink('composer-setup.php');" |
# Hello World
app/console generate:bundle | |
Welcome to the Symfony bundle generator! | |
Are you planning on sharing this bundle across multiple applications? [no]: | |
Your application code must be written in bundles. This command helps | |
you generate them easily. | |
Give your bundle a descriptive name, like BlogBundle. | |
Bundle name: BlogBundle | |
Bundles are usually generated into the src/ directory. Unless you're | |
doing something custom, hit enter to keep this default! | |
Target Directory [src/]: | |
What format do you want to use for your generated configuration? | |
Configuration format (annotation, yml, xml, php) [annotation]: | |
Bundle generation | |
> Generating a sample bundle skeleton into app/../src/BlogBundle | |
created ./app/../src/BlogBundle/ | |
created ./app/../src/BlogBundle/BlogBundle.php | |
created ./app/../src/BlogBundle/Controller/ | |
created ./app/../src/BlogBundle/Controller/DefaultController.php | |
created ./app/../tests/BlogBundle/Controller/ | |
created ./app/../tests/BlogBundle/Controller/DefaultControllerTest.php | |
created ./app/../src/BlogBundle/Resources/views/Default/ | |
created ./app/../src/BlogBundle/Resources/views/Default/index.html.twig | |
created ./app/../src/BlogBundle/Resources/config/ | |
created ./app/../src/BlogBundle/Resources/config/services.yml | |
> Checking that the bundle is autoloaded | |
> Enabling the bundle inside app/AppKernel.php | |
updated ./app/AppKernel.php | |
> Importing the bundle's routes from the app/config/routing.yml file | |
updated ./app/config/routing.yml | |
> Importing the bundle's services.yml from the app/config/config.yml file | |
updated ./app/config/config.yml | |
Everything is OK! Now get to work :). |
路由建议:
常访问的路由放前面,不常访问的路由放后面,可以提高效率
路由定义的两种常用写法:
1.Annotation,允许在方法的上面以注释形式定义方法运行状态,适用于方法定义清晰统一时
/** | |
* @Route("hi/{name}") | |
*/ |
2.routing.yml,symfony2 常用的配置格式
#app/config/routing.yml 中注册 | |
blog: | |
resource: "@BlogBundle/Resources/config/routing.yml" | |
prefix: / | |
#src/BlogBundle/Resources/config/routing.yml 中添加具体路由 | |
blog: | |
pattern: /hi/{name} | |
defaults: { _controller: BlogBundle:Default:index } |
路由匹配查找
app/console router:match /hi/{name} |
# Controller
Request
Response
Session
# Twig
基本语法
核心概念:用类的继承概念关系去管理页面之间的关系
继承 复写