Home > Backend Development > PHP Tutorial > symfony2 quick guide (2)

symfony2 quick guide (2)

WBOY
Release: 2016-08-08 09:24:12
Original
996 people have browsed it

Bundle in symfony2

symfony introduced the concept of Bundle in its second version. According to my current superficial understanding, the bundle here can be regarded as an abstraction of the web service you create. In addition, bundle can also be used as a library concept for other people to use. If you search "symfony2 introduces third-party bundle" online, you can see that there are many tutorials on how to do this.

Okay, what I want to write today is not to tell you how to write a bundle for others to use; it is to create a bundle for accessing the page.

Create your own bundle

symfony2 is a rapid development tool. It provides many tools that can automatically generate something that conforms to symfony2 itself, thus eliminating the trouble of website builders. To create a bundle, you can use the following command:

<code><span>php</span><span>app/console</span><span>generate:bundle</span><span>-</span><span>-</span><span>namespace=Blogger/BlogBundle</span><span>-</span><span>-</span><span>format=yml</span></code>
Copy after login

A very simple command, isn’t it?

–namespace is followed by the name of the project you want to create, and the name of the bundle. After creation, the corresponding generated code and files will be saved under ./src/Blogger/BlogBundle.

–format=yml This sentence will be formulated. When creating the controller, use the yml configuration file to configure the url–controller route. In addition to this method, symfony2 also provides annotation method to define routes. I believe that students who have used Java Spring will be familiar with annotation. But I personally prefer to use the configuration file method, because this method is more centralized and easier to debug. The annotation mode puts route definitions and code together. Although it seems to reduce the number of files in the project, you need to constantly search in the project to locate the corresponding relationship between your URL and your controller. Through configuration files, these things become clear at a glance.

In my project, I only use the annotation method in the entity, and the configuration file method is used in everything else.

File Shelf Structure

Folder Description
Controller contains the code of all controllers, which are used to handle requests from clients.
Entity Each entity represents a data table in a database.
Form form class, this class is mainly used to generate html forms for an entity, for subsequent additions, deletions, modifications and checks.
Resource This contains all js/css/web pages/documents.
Security If you need to have fine-grained control over user access, then the conditions here need to be adjusted accordingly. Since I didn't use this much this time, I won't introduce it too much.
Tests Obviously, everything inside is for testing. Symfony2 is tested using phpunit. If it is not installed, you still need to install it manually.
Validator This contains verification information for some fields. This time, my form contains input of json data, and I added json schema here to validate the json data entered by the user.

创建数据库实体对象

就像创建一个bundle一样,创建数据库实体也有相应的命令。

<code>php app/console doctrine:<span>generate</span>:<span>entity</span></code>
Copy after login

该命令是交互式的,也就是说运行过程中你需要回答它提出的一些问题。它会根据你的回答来创建你所需要的实体。
我这里的情况比较简单,根据提示,把相应的数据表的各个字段输入进去就可以了。

数据库对象的增删改查

<code>php app/console <span>generate</span>:doctrine:crud</code>
Copy after login

别的不多说,就这一条命令搞定。
可以看到symfony2使用的是doctrine来操作数据的。所以在你的symfony2项目开发中,少不了在网上搜索一些关于doctrine的东西,在这里就不多说了。等项目终于到了,再慢慢了解吧。
不过需要再说一句的是,当你在运行这个项目前,请确认你在数据库中已经创建了相应的数据库跟数据表,如果没有,可以使用下面这两个命令。

<code>php app/console doctrine:database:<span><span>create</span>
#根据所创建的实体,来在数据库中创建数据表
php app/console doctrine:<span>schema</span>:<span>update</span> --force</span></code>
Copy after login

项目运行

好了,到现在为止,你一行代码没有写。尝试着运行下自己的项目看看吧。

<code>php app/console <span>server</span>:run</code>
Copy after login

前后端分离 , asset管理

在你的项目开发中,免不了希望前后端分离的。即使没有前后端分离,你可能会引入一些css或者js的库来对你的网页进行美化。

在symfony2中这些都是通过asset这个库来管理的。
如果你是按照我上面这个步骤来创建项目的,那么在app/config/config.yml中你会发现一个叫做“assetic configuration”的字段。

<code>#Assetic Configuration
assetic:
    ......
    assets:
        base_lib_js:
            inputs:
                -<span><span>'@BloggerBlogBundler/Resources/public/js/jquery.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/bootstrap/js/bootstrap.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/bootstrap/js/bootstrap-table.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/angular/angular.min.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/angular/angular-locale_zh-cn.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/angular/angular-resource.min.js'</span></span>                -<span><span>'@BloggerBlogBundler/Resources/public/lib/angular/ui-bootstrap-tpls.min.js'</span></span></code>
Copy after login

不要忘记把这些库也放到相应的文件夹下。

好了,再次运行你的项目前不要忘了

<code>php app/console asset:<span>dump</span></code>
Copy after login

这条命令会把一些使用到css或者js文件拷贝到你的web目录下,毕竟web目录才是你的项目的入口。

千万不要忘记,每一次更新css或者js时,都要执行一下上面的命令,否则你的更新将不会生效。

–env

上面的每条命令都接受--env这个选项,用来指定是生产环境还是开发环境。如果没有指定的话,默认是开发环境。

拿上面的asset命令来说。

<code><span>php app/console asset:dump --env</span>=<span>prod</span></code>
Copy after login

该命令对开发环境进行asset更新。

项目部署

好了,项目开发完了,你不会再使用symfony2自带的这个web服务器了。需要注意的是你的web文件夹才是nginx需要的root地址。

以上就介绍了symfony2快速指南(二),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template