Table of Contents
High-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial
I asked: Which PHP framework and which CMS should be learned by PHP beginners?
Things about getting started with the php framework
Home Backend Development PHP Tutorial High-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial_PHP tutorial

High-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial_PHP tutorial

Jul 13, 2016 am 10:23 AM
php framework getting Started

High-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial

Symfony2 is a web development framework based on PHP language, which has the characteristics of fast development speed and high performance. This article describes the configuration and program development of the Symfony2 framework in detail through the implementation process of a program example.

1. Download

First download Symfony2, go to http://symfony.com/download or download from this site http://www.bkjia.com/codes/187833.html. I take Ubuntu system as an example, use .tgz compression package, decompress the source file to the /var/www directory and execute:

tar zxvf Symfony_Standard_Vendors_2.0.###.tgz -C /var/www

Copy after login

The ### above refers to the version number. When I downloaded it, it was BETA5.

After unzipping, the directory of Symfony2 is as follows:

/var/www/ <- Web根目录 
 Symfony/ <- Symfony2解压目录 
  app/ <- 存放symfony的核心文件的目录
   cache/ <- 存放缓存文件的目录
   config/ <- 存放应用程序全局配置的目录
   logs/ <- 存放日志的目录
  src/ <- 应用程序源代码
   ... 
  vendor/ <- 供应商或第三方的模组和插件
   ... 
  web/ <- Web入口
   app.php <- 生产环境下的前端控制器
   ... 

Copy after login

If you need to install (if you downloaded the without vendor version) or update vendor (third-party) content, you can use:

cd /var/www/Symfony
php bin/vendors install

Copy after login

2. Configuration

The configuration of Symfony2 is very simple, just enter in the browser:

http://localhost/Symfony/web/config.php

Copy after login

Then just follow the prompts. What is worth noting is the permission issue of the app/cache and app/logs directories. Since I installed it under Ubuntu, I can use it (firehare is my user name, you can replace it with your user name here):

#为了保险起见 
rm -rf app/cache/* 
rm -rf app/logs/* 
#设置ACL 
sudo setfacl -R -m u:www-data:rwx -m u:firehare:rwx app/cache app/logs 
sudo setfacl -dR -m u:www-data:rwx -m u:firehare:rwx app/cache app/logs 

Copy after login

If the system does not support the setfacl command, there are two places to check:
Is setfacl already installed? If not, you can install it through the following command (it seems to be installed by default in Ubuntu 11.10, and the package is called acl):

sudo apt-get install setfacl 
Copy after login

If setfacl has been installed, please check the /etc/fstab file to see if the acl option has been added:

# /var was on /dev/sda7 during installation 
UUID=c2cc4104-b421-479a-b21a-1108f8895110 /var ext4 defaults,acl 0 2 

Copy after login

Then fill in the database name and other information according to the page prompts, and then copy this information to the /var/www/Symfony/app/config/parameters.ini file, as shown below:

; These parameters can be imported into other config files 
; by enclosing the key with % (like %database_user%) 
; Comments start with ';', as in php.ini 
[parameters] 
 database_driver="pdo_mysql" 
 database_host="localhost" 
 database_name="symfony" 
 database_user="symfony" 
 database_password="symfony" 
 mailer_transport="smtp" 
 mailer_host="localhost" 
 mailer_user="" 
 mailer_password="" 
 locale="zh_CN" 
 secret="29f96e9e70c2797cb77dd088d3954d3c38d9b33f" 
Copy after login

 
If everything is OK, you will get a Demo page when you enter the following address in your browser:

http://localhost/Symfony/web/app_dev.php

Copy after login

3. Program example:

1.Create Bundle:

First create a Bundle:

php app/console gen:bundle "AcmeHelloBundle" src
  为了确保Acme名称空间可以被自动加载,请在你的app/autoload.php文件添加下列语句:
$loader->registerNamespaces(array( 
 // ...
 //添加自定义的名称空间 
 'Acme' => __DIR__.'/../src', 
 // ... 
)); 
  最后是将该Bundle注册到Symfony2中,请在你的app/AppKernel.php文件中添加下列语句:
// app/AppKernel.php 
public function registerBundles() 
{ 
 $bundles = array( 
  // ... 
  new AcmeHelloBundleAcmeHelloBundle(), 
 ); 
 
 // ... 
 
 return $bundles; 
} 
Copy after login

2. Create route

Routing can be created in app/config/routing.yml, but in order to have good programming habits and code organization, it can be placed in Resources/config/routing.yml in the created Bundle directory, and in Only the reference to the routing file is retained in app/config/routing.yml, as shown below:

# app/config/routing.yml 
homepage: 
 pattern: / 
 defaults: { _controller: FrameworkBundle:Default:index } 
hello: 
 resource: "@AcmeHelloBundle/Resources/config/routing.yml"

Copy after login

The real routing is written in the src/Acme/HelloBundle/Resources/config/routing.yml routing file, as shown below:

# src/Acme/HelloBundle/Resources/config/routing.yml 
hello: 
 pattern: /hello/{name} 
 defaults: { _controller: AcmeHelloBundle:Hello:index, name:'pu' }

Copy after login

3.Create controller:

The name of the controller must be HelloController.php. The reason is very simple, because your routing has already given the name of the controller. The controllers in lines 4 and 7 of the routing file above are both It starts with AcmeHelloBundle:Hello, where AcmeHelloBundle represents the Bundle name, and Hello represents the controller name, so the controller must be HelloController.php, and the Controller name extension is the naming convention. As for the subsequent index and say, they are methods in the controller class. The index method is defined below. Of course, the method name is indexAction, which is also a naming convention:

// src/Acme/HelloBundle/Controller/HelloController.php 
namespace AcmeHelloBundleController; 
use SymfonyComponentHttpFoundationResponse; 
class HelloController 
{ 
 public function indexAction($name) 
 { 
  return new Response('<html><body>Hello '.$name.'!</body></html>'); 
 } 
} 
Copy after login

In this way, when we enter

in the browser
http://localhost/hello/index/World

Copy after login

The words Hello World! will be displayed.

4.Create template:

To be able to reuse blocks in layout files, templates can be used to replace HTML statements in controllers. First create the page layout file:

{# app/Resources/views/layout.html.twig #} 
<!DOCTYPE html> 
<html> 
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title>{% block title %}Hello Application{% endblock %}</title> 
 </head> 
 <body> 
  {% block body %}{% endblock %} 
 </body> 
</html> 

Copy after login

Note that this file is located in the app/Resources/views/ directory, and its scope is the global template file of the entire application. Two blocks are defined in this file: title and body. The next step is to create a template dedicated to the Hello controller, as shown below:

{# src/Acme/HelloBundle/Resources/views/Hello/index.html.twig #} 
{% extends '::layout.html.twig' %} 
{% block body %} 
 Hello {{ name }}! 
{% endblock %} 

Copy after login

In this file, it inherits the global template and defines the block body, thus overriding the body block in the global template. If the system renders this template, it will overwrite the block body of the global template with the block body and then render it.
Finally, change the HTML statement in the controller to render the above template:

// src/Acme/HelloBundle/Controller/HelloController.php 
namespace AcmeHelloBundleController; 
use SymfonyBundleFrameworkBundleControllerController; 
class HelloController extends Controller 
{ 
 public function indexAction($name) 
 { 
  return $this->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name)); 
 } 
}
Copy after login

I asked: Which PHP framework and which CMS should be learned by PHP beginners?

Nowadays, the mainstream use of Dreamweaver and Empire CMS
I personally prefer Empire CMS

Beginners of the PHP framework recommend that you use the integrated environment to install PHPNOW or APMServ with one click
There is no need to make your computer too complicated

Things about getting started with the php framework

Since it is a big project, it is recommended not to use a framework. If you use it, you can consider zendframework or thinkphp. At the same time, please note that smarty does not belong to the framework. It is recommended to use smarty as the template processing mechanism you develop

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840749.htmlTechArticleHigh-performance PHP framework Symfony2 classic introductory tutorial, symfony2 introductory tutorial Symfony2 is a Web development framework based on PHP language, with Features include fast development speed and high performance. This article is based on a process...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Aug 01, 2024 pm 03:28 PM

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award Jun 20, 2024 pm 05:43 PM

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

From bare metal to a large model with 70 billion parameters, here is a tutorial and ready-to-use scripts From bare metal to a large model with 70 billion parameters, here is a tutorial and ready-to-use scripts Jul 24, 2024 pm 08:13 PM

We know that LLM is trained on large-scale computer clusters using massive data. This site has introduced many methods and technologies used to assist and improve the LLM training process. Today, what we want to share is an article that goes deep into the underlying technology and introduces how to turn a bunch of "bare metals" without even an operating system into a computer cluster for training LLM. This article comes from Imbue, an AI startup that strives to achieve general intelligence by understanding how machines think. Of course, turning a bunch of "bare metal" without an operating system into a computer cluster for training LLM is not an easy process, full of exploration and trial and error, but Imbue finally successfully trained an LLM with 70 billion parameters. and in the process accumulate

AI in use | AI created a life vlog of a girl living alone, which received tens of thousands of likes in 3 days AI in use | AI created a life vlog of a girl living alone, which received tens of thousands of likes in 3 days Aug 07, 2024 pm 10:53 PM

Editor of the Machine Power Report: Yang Wen The wave of artificial intelligence represented by large models and AIGC has been quietly changing the way we live and work, but most people still don’t know how to use it. Therefore, we have launched the "AI in Use" column to introduce in detail how to use AI through intuitive, interesting and concise artificial intelligence use cases and stimulate everyone's thinking. We also welcome readers to submit innovative, hands-on use cases. Video link: https://mp.weixin.qq.com/s/2hX_i7li3RqdE4u016yGhQ Recently, the life vlog of a girl living alone became popular on Xiaohongshu. An illustration-style animation, coupled with a few healing words, can be easily picked up in just a few days.

Counting down the 12 pain points of RAG, NVIDIA senior architect teaches solutions Counting down the 12 pain points of RAG, NVIDIA senior architect teaches solutions Jul 11, 2024 pm 01:53 PM

Retrieval-augmented generation (RAG) is a technique that uses retrieval to boost language models. Specifically, before a language model generates an answer, it retrieves relevant information from an extensive document database and then uses this information to guide the generation process. This technology can greatly improve the accuracy and relevance of content, effectively alleviate the problem of hallucinations, increase the speed of knowledge update, and enhance the traceability of content generation. RAG is undoubtedly one of the most exciting areas of artificial intelligence research. For more details about RAG, please refer to the column article on this site "What are the new developments in RAG, which specializes in making up for the shortcomings of large models?" This review explains it clearly." But RAG is not perfect, and users often encounter some "pain points" when using it. Recently, NVIDIA’s advanced generative AI solution

Comparison of the advantages and disadvantages of PHP frameworks: Which one is better? Comparison of the advantages and disadvantages of PHP frameworks: Which one is better? Jun 04, 2024 pm 03:36 PM

The choice of PHP framework depends on project needs and developer skills: Laravel: rich in features and active community, but has a steep learning curve and high performance overhead. CodeIgniter: lightweight and easy to extend, but has limited functionality and less documentation. Symfony: Modular, strong community, but complex, performance issues. ZendFramework: enterprise-grade, stable and reliable, but bulky and expensive to license. Slim: micro-framework, fast, but with limited functionality and a steep learning curve.

Performance differences of PHP frameworks in different development environments Performance differences of PHP frameworks in different development environments Jun 05, 2024 pm 08:57 PM

There are differences in the performance of PHP frameworks in different development environments. Development environments (such as local Apache servers) suffer from lower framework performance due to factors such as lower local server performance and debugging tools. In contrast, a production environment (such as a fully functional production server) with more powerful servers and optimized configurations allows the framework to perform significantly better.

PHP Frameworks and Microservices: Cloud Native Deployment and Containerization PHP Frameworks and Microservices: Cloud Native Deployment and Containerization Jun 04, 2024 pm 12:48 PM

Benefits of combining PHP framework with microservices: Scalability: Easily extend the application, add new features or handle more load. Flexibility: Microservices are deployed and maintained independently, making it easier to make changes and updates. High availability: The failure of one microservice does not affect other parts, ensuring higher availability. Practical case: Deploying microservices using Laravel and Kubernetes Steps: Create a Laravel project. Define microservice controllers. Create Dockerfile. Create a Kubernetes manifest. Deploy microservices. Test microservices.

See all articles