Symfony2 Framework creation Project and template Setup instance detailed _php instance

Source: Internet
Author: User
Tags php class php template wrapper blog layout

The examples in this article describe how the SYMFONY2 framework creates projects and template settings. Share to everyone for your reference, specific as follows:

Environmental preparedness and overview

Accustomed to using the Netbean Editor in Windows and using VirtualBox virtual CentOS system, Nginx+php-fpm+mysql, of course Apache is also a good choice to use http:// Symfony on Windows and CentOS as the development domain name.

First, download and environment settings

1. How to build a development environment on the CentOS is no longer detailed, of course, you can build a development environment on Windows.

2. Regarding the use of symfony instead of 127.0.0.1 to modify/etc/hosts files in the Liunx system, modify Win7 files in C:\Windows\System32\drivers\etc\host system (required to open with Administrator privileges)

Add content similar to IP alias 1 alias 2, such as:

Copy Code code as follows:
#/etc/hosts 127.0.0.1 Symblog Dev symfony

3. Manually download 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 is that the App/cache and App/logs directories need to be set to 777 permissions. This problem should not exist in the development environment of Windows.

4. Modify the Apache or Nginx profile symfony domain name to point to the downloaded Symfony file's web directory.

At this point you should be able to access the default page of the Symfony through http://symfony/app_dev.php, there are several demo can refer to learning.

app_dev.php The default loading of a development tool bar below, shows some information on the current page, greatly facilitates the debugging of the program, only when the environment variable for Dev will be displayed.

5. Use composer installation, will be prompted to output MySQL and other related information, need to modify this information, or directly download the file, you can enter the page "Configure" for the relevant settings.

Bundles (may be called package, bundle, assembly, or project, or in English) is the basis of symfony, one by one to share the reusable code encapsulation, and 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 php5.3 after the namespace, most of the cpenal, da virtual host as if only the php5.2 version of it, can not run Symfony2.

Second, create a bundle

A blog is created in the following example, 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 Code code as follows:
PHP app/console generate:bundle–namespace=blogger/blogbundle–format=yml

You can use all the default settings directly after running. We can easily create the basic controllers, modules and templates that we need. Contains the following behavior:

Register Bundles

All bundles used in symfony are required to be registered first, and some bundles will only be used to develop the test environment (dev or test), as mentioned in the previous development tool bar. The following break code shows how the bundles create command registers Bloggerblogbundle this bundle.

App/appkernel.php
class Appkernel extends Kernel {public
function registerbundles () {
$bundles = array ( c5/>//.
. New Blogger\blogbundle\bloggerblogbundle (),
);
// .. return $bundles; } // ..
}
}

Routing

As a framework, the routing function is created by the Bundler creator in App/config/routing.yml, symfony the configuration information is saved in YML format.

Copy Code code as follows:

# APP/CONFIG/ROUTING.YML
Bloggerblogbundle:
Resource: "@BloggerBlogBundle/resources/config/routing.yml"
Prefix:/

The prefix prefix option allows us to place them in subdirectories 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. Create a blogger directory under SRC, and have blogbundle subdirectories to store a variety of related stuff. The difference is that a blogger-like directory corresponds to the PHP namespace.

Default Controller

The bundle generator generates a default controller under SRC. By visiting: Http://symfony/app_dev.php/hello/world can see simple greetings. about how this page is generated:

Routing

or routing, the difference is that the previous route is registered in the entire program, where the route is to control the use of specific pages, SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML Controls the Bloggerblogbundle, including the following program fragments:

Copy Code code as follows:

# SRC/BLOGGER/BLOGBUNDLE/RESOURCES/CONFIG/ROUTING.YML
Bloggerblogbundle_homepage:
Pattern:/hello/{name}
Defaults: {_controller:bloggerblogbundle:default:index}

Parameters: For URL detection, any value that conforms to the/HELLO/{NAME} structure will be given to {name},
Mode: There is no restriction on the form, theory can be put, get, post, delete all operations can be carried out.
Follow-up: If the above two is met, then {name} will be transmitted to a specific file, 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 is transmitted to the template file:

src/blogger/blogbundle/controller/defaultcontroller.php
namespace 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 Index.html.twig template file under the Default folder in the Bloggerblogbundle Views folder.

Template file

Open the above template file, a very simple sentence code:

{# Src/blogger/blogbundle/resources/views/default/index.html.twig #}
Hello {{Name}}!

The above is the entire MVC process of symfony, and so many files are simply outputting a "Hello World". In theory, you do not need to bundler the creator, just manually creating the above file can also achieve the same effect. It's going to take a lot of time.

Back to the point, we're creating a blog system so we don't need Hello world,

1. Remove Controller src/blogger/blogbundle/controller/defaultcontroller.php
2. Remove Template src/blogger/blogbundle/resources/views/default/
3. Final removal of routing src/blogger/blogbundle/resources/config/routing.yml
The whole world is quiet.

Third, let us start to create 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: It is a compiled PHP class, can occupy less resources

2. Concise: Think about to play <?php, twig input content is much less.

3. Inheritable: A function of very cool

4. Security: Escape features are turned on by default and can even provide sandbox functionality for important code.

5. Scalable: Additional customization features are required and can be extended at any time

For more information, please contact: http://twig.sensiolabs.org/

Inheritable is a very good advantage, we will use the level three inheritance structure to customize this template, which will allow us to modify the template at three different levels to facilitate the free customization.

Main template –level 1

<!–app/Resources/views/base.html.twig–> <! DOCTYPE html>  

The code above introduces a JS file, implements HTML in the browser before the IE9 version, and imports two CSS files into Google fronts.
This constitutes the main content structure of the Web page, equivalent to the html.tpl.php+page.tpl.php of Drupal.
Let's take a look at the header file.

Copy Code code as follows:
<title>{% block title%}blog{% Endblock%}–symfony</title>

{The% label is not HTML, nor is it PHP, which is one of 3 twig tags used to perform certain actions. Here you can find the complete twig control action for this tag. Back to the current code, is used to find the title block and output him, if not the output of the default Symblo word.
The twig can be used to modify title, which we can override in other template files:

{% extends ':: Base.html.twig '%}
{% block title%} The blog title goes here{% endblock%}

The code above first inherits the file that first defined the block, and then modifies it. The title section of the site 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 bundle and controller, and not including these two fields directly references app/resources/views/ The file below the folder.

In the CSS stylesheet, we can find another twig tag {{, this is an output (say something) tag.

Copy Code code as follows:
<link href= "{{asset (' Css/screen.css ')}}" Type= "Text/css" rel= "stylesheet"/>

This tag is used to output variables or expressions, and the example above outputs the return value of the asset function, which provides a portable way to return CSS,JS, the image's address.

This tag can be output in a specific format we want content, such as time:

Copy Code code as follows:
{{blog.created|date (' D-m-y ')}}

The full list of filters can be found in the Twig document.

The last tag does not appear in the above code, it is {#, just a comment label

Copy Code code as follows:
{# annotation content can be exported here #}

Next we'll create CSS stylesheet Web/css/screen.css and add the following.

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% 0%; Color: #000; font-size:14px; {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;} #h Eader. Navigation li {display:inline} #header. Navigation li a {display:inline-block; padding:10px 15px; border-left : 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-bott om:20px; Font-weight: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 templates for blog bundler, create Src/blogger/blogbundle/resources/views/layout.html.twig and join:

Copy 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. Inherits the first level template, and specially customized the sidebar for the blog content, obviously we don't 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 Layout –level 3

This phase has been involved in creating specific pages, so you need to create a controller first src/blogger/blogbundle/controller/pagecontroller.php

src/blogger/blogbundle/controller/pagecontroller.php
namespace 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 Code code as follows:

{# Src/blogger/blogbundle/resources/views/page/index.html.twig #}
{% extends ' Bloggerblogbundle::layout.html.twig '%}
{% block body%}
Blog Homepage
{% Endblock%}

Create a route to lead the homepage to the page we just created: src/blogger/blogbundle/resources/config/routing.yml

Copy 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 home page.

Four, and then create an about page

Routing: adding in Src/blogger/blogbundle/resources/config/routing.yml

Copy 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 in the Bloggerblogbundle namespace when you access the About page in get mode.

Controller: Add the About action in the src/blogger/blogbundle/controller/pagecontroller.php to the page controller

Copy 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 Code code as follows:

{# Src/blogger/blogbundle/resources/views/page/about.html.twig #}
{% extends ' Bloggerblogbundle::layout.html.twig '%}
{% block body%}
About page
{% Endblock%}

Simple three processes added to the page: Http://symfony/app_dev.php/about

I hope this article will help you with the PHP program design based on Symfony framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.