Symfony2 framework Create project and template setup examples in detail, SYMFONY2 framework
This example describes how the SYMFONY2 framework creates project and template settings. Share to everyone for your reference, as follows:
Environment Preparation and overview
Accustomed to using the Netbean Editor in Windows and using the VirtualBox virtual CentOS system, pre-nginx+php-fpm+mysql, of course, Apache is also a good choice, using HTTP// Symfony is developed as a domain name on windows and CentOS.
First, download and environment settings
1. How to build a development environment on CentOS is no longer detailed, but you can also build a development environment on Windows.
2. About using Symfony instead of 127.0.0.1 to modify the/etc/hosts file in the Liunx system, modify the Win7 file in the C:\Windows\System32\drivers\etc\host system (need to open with Administrator privileges)
Add content similar to the IP alias 1 alias 2, such as:
Copy the Code code as follows: #/etc/hosts 127.0.0.1 Symblog Dev symfony
3. Manually download the Symfony2, you can also refer to this page with composer for installation. Http://symfony.com/doc/current/book/installation.html
The only thing to note: The App/cache and app/logs directories need to be set to 777 permissions. The Windows development environment should not exist for this issue.
4. Modify the Apache or nginx configuration file symfony domain name to the web directory of the downloaded Symfony file.
You should be able to access the default page of Symfony via http://symfony/app_dev.php, and there are several demos that can be referenced to learn.
app_dev.php A developer toolbar is loaded by default, which shows some information about the current page, which greatly facilitates debugging of the program, which is displayed only when the environment variable is dev.
5. When using composer installation, you will be prompted to output MySQL and other related information, need to modify this information, or directly download the file, you can go to the page "Configure" for the relevant settings.
Bundles (perhaps referred to as package, bundle, assembly, or project, or English bar) is the basis of symfony, one by one to share a reusable code package, even symfony itself as a Bundles run. including controllers, modules, templates, and even images and JS, CSS style sheets and other resources. Very messy things, different bundles use the php5.3 after the namespace, most cpenal, da virtual host as if only php5.2 version of it, can not run Symfony2.
Second, create a bundle
In the following example, a blog will be created, and Symfony provides a number of tools to quickly create a project. For example, we can use it to quickly create a blog base bundle.
Copy the Code code as follows: PHP app/console generate:bundle–namespace=blogger/blogbundle–format=yml
After running, use all the default settings directly. It is easy to create the basic controller, modules and templates we need. The following behaviors are included:
Register Bundles
All bundles used in symfony are required to be registered first, and some bundles will only be used for development test environments (dev or test), as mentioned in the previous Development toolbar. The following code shows how the bundles creation command registers the Bloggerblogbundle bundle.
App/appkernel.phpclass Appkernel extends Kernel {public Function registerbundles () {$bundles = array (//). New Blogger\blogbundle\bloggerblogbundle (),);//.. return $bundles; } // ..}}
Routing
As a framework, the routing feature was created by the Bundler creator in App/config/routing.yml, Symfony is to save configuration information in YML format.
Copy the Code code as follows:
# APP/CONFIG/ROUTING.YML
Bloggerblogbundle:
Resource: "@BloggerBlogBundle/resources/config/routing.yml"
Prefix:/
The prefix prefix option allows us to place it in sub-directories such as blogs, news, and so on.
File
In addition to the above configuration files, most of the other files are born into the SRC directory, like most MVC frameworks. The blogger directory is generated under SRC, and there is a blogbundle subdirectory that stores all kinds of related stuff. The difference is that the blogger-like directory corresponds to the PHP namespace.
Default Controller
The bundle generator generates the default controller under SRC. By visiting: Http://symfony/app_dev.php/hello/world can see a simple greeting. about how this page is generated:
Routing
or routing, the difference is that the previous route is registered in the entire program to use, where the route is to control the use of specific pages, SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML Controls the Bloggerblogbundle, which contains the following program fragments:
Copy the Code code as follows:
# SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML
Bloggerblogbundle_homepage:
Pattern:/hello/{name}
Defaults: {_controller:bloggerblogbundle:default:index}
Parameters: URL detection, any value conforming to the/HELLO/{NAME} structure will be assigned to {name},
Way: There is no restriction on the form, the theory can put, get, post, delete all the operations can be done.
Subsequent: If the above two is met, then {name} will be transmitted to a specific file, the above is src/blogger/blogbundle/controller/ The index behavior of the default controller in the defaultcontroller.php file will be used.
Controller
In the default production bundler, the controller behaves fairly simply, and the {name} parameter is passed in and sent out to the template file:
Src/blogger/blogbundle/controller/defaultcontroller.phpnamespace Blogger\blogbundle\controller;use Symfony\ Bundle\frameworkbundle\controller\controller;class Defaultcontroller extends Controller{public function indexAction ($name) {return $this->render (' BloggerBlogBundle:Default:index.html.twig ', Array (' name ' = = $name)}}
BloggerBlogBundle:Default:index.html.twig will use the Bloggerblogbundle Views folder under the Default folder under the Index.html.twig template file.
Template file
Open the template file above, a very simple code:
{# Src/blogger/blogbundle/resources/views/default/index.html.twig #} Hello {{Name}}!
The above is the whole MVC process of symfony, so many files just output a "Hello World". In theory, you do not need to bundler the creator, but you can achieve the same effect by manually creating the above file. It's going to take a lot of time.
Back to the point, we're creating a blogging system, so don't need Hello world,
1. Remove the controller src/blogger/blogbundle/controller/defaultcontroller.php
2. Removing the template src/blogger/blogbundle/resources/views/default/
3. Finally remove the route src/blogger/blogbundle/resources/config/routing.yml
The whole world is quiet.
Third, let's start creating the homepage of the blog
Advantages of Twig
In Symfony we can use twig and PHP (this is not nonsense) as a template. The following advantages of using twig:
1. Fast: Is a Yi php class, can occupy less resources
2. Concise: Think about playing <?php, twig input much less content.
3. Inheritable: A very cool feature
4. Security: The escape feature is turned on by default and even provides sandbox functionality for important code.
5. Expandable: Additional customization is required and can be extended at any time
For more information, please visit: http://twig.sensiolabs.org/
Inheritable is a very good advantage, we will use the three-level inheritance structure to customize this template, which will allow us to modify the template at three different levels to facilitate free customization.
Main template –level 1
<title>{% block title%}symfony{% Endblock%}–blog</title>
{% block stylesheets%}
{% Endblock%}
{% block navigation%}
{% Endblock%}
{% block Blog_title%}symfony{% endblock%}
{% block blog_tagline%}creating a blog in symfony2{% endblock%}
{% block body%}{% endblock%}
{% block sidebar%}{% endblock%}{% block Footer%}symfony2 Blog Tutorial {% E Ndblock%}
{% block javascripts%} {% Endblock%}
The above code introduced a JS file, implemented HTML in the browser before the IE9 version, and two CSS files were imported into Google fronts.
This constitutes the main content structure of the Web page, which is equivalent to Drupal's html.tpl.php+page.tpl.php.
Let's take a look at the head file.
Copy the Code code as follows:{% block title%}blog{% Endblock%}–symfony
{The% tag is not HTML, nor PHP, he is one of the 3 twig tags used to perform certain actions. Here you can find the complete twig control action for this label. Return to the current code, is used to find the title of the block and output him, if not, the default Symblo the word output.
The twig feature can be used to modify the title, and we can rewrite it in other template files:
{% extends ':: Base.html.twig '%}
{% block title%} The blog title goes here{% endblock%}
The above code first inherits the file that defines the block for the first time, and then modifies it. The title section of the website will output ' the blog title Goes Here–symfony '.
In general, we use Bundle:controller:template when referencing template files, but the above code does not have bundles and controllers, and these two fields are directly referenced app/resources/views/ Folder below the file.
In the CSS style sheet, we can find another twig tag {{, which is an output (say something) tag.
Copy the Code code as follows:
This tag is used to output a variable or an expression, the above example outputs the return value of the asset function, which provides a portable way to return the address of the CSS,JS, the image.
This tag can output the content we want in a specific format, such as time:
Copy the Code Code as follows: {{blog.created|date ("D-m-y")}}
All filter lists can be found in the Twig documentation.
The last tag does not appear in the above code, it is {#, just a comment tag
Copy the Code Code as follows: {# comment content can be output here #}
Next we will create a CSS style sheet web/css/screen.css, adding the following content.
Html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite, Code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li, Fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure, Figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{border:0;font-size : 100%;font:inherit;vertical-align:baseline;margin:0;padding:0}article,aside,details,figcaption,figure,footer, Header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none }blockquote:before,blockquote:after,q:before,q:after{content:none}table{border-collapse:collapse; border-spacing:0}body {line-height:1;font-family:arial, Helvetica, sans-serif;font-size:12px; width:100%; height:10 0%; Color: #000; font-size:14px; }.clear {Clear:both;} #wrapper {margin:10px auto; width:1000px;} #wrapper a {text-decoration:None Color: #F48A00; } #wrapper span.highlight {color: #F48A00;} #header {border-bottom:1px solid #ccc; margin-bottom:20px;} #header. Top {border-bottom:1px solid #ccc; margin-bottom:10px;} #header ul.navigation {list-style:none; text-align:right;} #header. Navigation li {display:inline} #header. Navigation li a {display:inline-block; padding:10px 15px; Border-lef t:1px solid #ccc; } #header H2 {font-family: ' Irish Grover ', cursive; font-size:92px; text-align:center; line-height:110px;} #header h2 a {color: #000;} #header h3 {text-align:center; font-family: ' La Belle Aurore ', cursive; font-size:24px; margin-bottom:20px; font-weigh T:normal; }.main-col {width:700px; display:inline-block; float:left; border-right:1px solid #ccc; padding:20px; Margin-bottom: 20px; }.sidebar {width:239px; padding:10px; display:inline-block;}. Main-col a {color: #F48A00;}. Main-col h1,.main-col h2{line-height:1.2em; font-size:32px; margin-bottom:10px; font-weight:normaL Color: #F48A00; }.main-col p {line-height:1.5em; margin-bottom:20px;} #footer {border-top:1px solid #ccc; clear:both; text-align:center; padding:10px; color: #aaa;}
Bundler Template –level 2
Now we create a template for blog bundler, create Src/blogger/blogbundle/resources/views/layout.html.twig and join:
Copy the Code code as follows:
{# Src/blogger/blogbundle/resources/views/layout.html.twig #}
{% extends ':: Base.html.twig '%}
{% block sidebar%}
Sidebar Content
{% Endblock%}
Very simple code, 1. Inheriting the first-level template and customizing the sidebar for the content of the blog, it is clear that we do not want the blog layout to be the same as other pages. Similar to the page–content-type.tpl.php template in Drupal7, the layout of a particular type of content is customized.
Specific page layouts –level 3
This phase already involves creating a specific page, so you need to create a controller first src/blogger/blogbundle/controller/pagecontroller.php
Src/blogger/blogbundle/controller/pagecontroller.phpnamespace Blogger\blogbundle\controller;use Symfony\Bundle \frameworkbundle\controller\controller;class Pagecontroller extends Controller{public function indexAction () {return $this->render (' BloggerBlogBundle:Page:index.html.twig ');}}
Then create the appropriate twig file: Src/blogger/blogbundle/resources/views/page/index.html.twig
Copy the Code code as follows:
{# Src/blogger/blogbundle/resources/views/page/index.html.twig #}
{% extends ' Bloggerblogbundle::layout.html.twig '%}
{% block body%}
Blog Homepage
{% Endblock%}
Creating a route directs the homepage to the page we just created: src/blogger/blogbundle/resources/config/routing.yml
Copy the Code code as follows:
# SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML
Bloggerblogbundle_homepage:
Pattern:/
Defaults: {_controller:bloggerblogbundle:page:index}
Requirements
_method:get
Visit http://symfony/app_dev.php again to see the simple homepage.
Iv. create a more about page
Routing: adding in Src/blogger/blogbundle/resources/config/routing.yml
Copy the Code code as follows:
# SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML
Bloggerblogbundle_about:
Pattern:/about
Defaults: {_controller:bloggerblogbundle:page:about}
Requirements
_method:get
Executes the page controller about action that is located in the Bloggerblogbundle namespace when the About page is accessed in get mode.
Controller: Add about action in src/blogger/blogbundle/controller/pagecontroller.php to page controller
Copy the Code code as follows:
src/blogger/blogbundle/controller/pagecontroller.php
// ..
Public Function aboutaction ()
{
return $this->render (' BloggerBlogBundle:Page:about.html.twig ');
}
// ..
Templates: Creating Src/blogger/blogbundle/resources/views/page/about.html.twig and adding related paging files
Copy the Code code as follows:
{# Src/blogger/blogbundle/resources/views/page/about.html.twig #}
{% extends ' Bloggerblogbundle::layout.html.twig '%}
{% block body%}
About page
{% Endblock%}
Three simple processes added about page: http://symfony/app_dev.php/about
It is hoped that this article is helpful to the PHP program design based on Symfony framework.
Articles you may be interested in:
- Symfony2 example Summary of database query method using doctrine
- Symfony2 a summary of how to achieve data acquisition from a database
- Symfony2 Creating a Page instance
- Summary of Symfony2 's session and cookie usage
- Symfony2 Framework Learning Notes Form usage
- Analysis of plugin format of Symfony2 learning notes
- Symfony2 Study Notes System routing
- Symfony2 study notes of the controller usage detailed
- Symfony2 Study Notes Template usage detailed
- An example analysis of controller usage of SYMFONY2 development
- Symfony2 installing a third-party bundles instance
- Symfony2 use third-party library upload to make picture upload instance detailed
- Implementation method of Symfony2 Federated query
http://www.bkjia.com/PHPjc/1111331.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111331.html techarticle SYMFONY2 Framework Create project and template setup examples in detail, SYMFONY2 framework This article describes how the SYMFONY2 framework creates project and template settings. Share to everyone for your reference, specific ...