symfony - RIP Tutorial

15
symfony #symfony

Transcript of symfony - RIP Tutorial

symfony

#symfony

1

1: symfony 2

2

2

2

2

Symfony 3 2

Symfony 2 2

Examples 3

SymfonySymfony 3

Linux / MacOSSymfony Installer 3

Symfony 3

Symfony 3

ComposerSymfony 3

Symfony 4

PHPWebSymfony 4

2: 5

5

5

5

Examples 5

5

Twig 5

404 6

Request 6

3: 7

7

Examples 7

7

4: 8

8

8

Examples 8

8

Request 8

POST 9

cookie 9

5: 10

10

10

Examples 10

10

10

11

12

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: symfony

It is an unofficial and free symfony ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official symfony.

The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]

https://riptutorial.com/zh-TW/home 1

1: symfony

SymfonyPHPSymfony Framework。

Symfony。

Symfony。。

SymfonyLaravelDrupalMagentoComposer。

Symfony。 。

SymfonySymfony 。

Symfony 3

3.3 07/2018 2017529

3.2 01/2018

3.1 07/2017

3.0 01/2017 20151130

Symfony 2

2.8 20151130

2.7 05/2019 2015530

2.6 01/2016

2.5 07/2015 2014531

2.4 01/2015 2013123

2.3 05/2017 201363

2.2 05/2014 2013-03-01

2.1 2012-09-06

2.0 09/2013 2011-07-28

https://riptutorial.com/zh-TW/home 2

Examples

SymfonySymfony

Symfony InstallerSymfony。PHP 5.4。

Linux / MacOSSymfony Installer

sudo mkdir -p /usr/local/bin sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony sudo chmod a+x /usr/local/bin/symfony

symfony。Symfony。

Symfony

Symfony。

symfony new my_project_name

Symfonymy_project_name 。ComposerSymfony。

Symfony

Symfonynew。

symfony new my_project_name 3.2

symfony new my_project_name 3.2.9

symfony new my_project 2.7.0-BETA1 symfony new my_project 2.7.0-RC1

LTS

symfony new my_project_name lts

ComposerSymfony

Symfony InstallerComposer。Composer 。

create-project

composer create-project symfony/framework-standard-edition my_project_name

https://riptutorial.com/zh-TW/home 3

Symfony Installermy_project_nameSymfony Standard Edition Symfony。

Symfony

Symfony InstallerSymfony

composer create-project symfony/framework-standard-edition my_project_name "2.8.*"

lts 。

PHPWebSymfony

Symfony server:runPHP WebWeb

cd my_project_name/ php bin/console server:run

http// localhost8000 /Symfony。

Web。WebApacheNginx。

symfony https://riptutorial.com/zh-TW/symfony/topic/9448/symfony

https://riptutorial.com/zh-TW/home 4

2:

SymfonyPHPHTTPHTTP。 HTTPHTMLJSON。

Symfony 。

$ this-> generateUrl'route_name'['placeholder'=>'value']; //route_nameURL•$ - >'template.html.twig'; //TwigResponse•$ this-> render'template.html.twig'['parameter'=> $ value]; //Twig•throw $ this-> createNotFoundException'Message'; //NotFoundHttpExceptionSymfony404•

HTTP。

Examples

// src/AppBundle/Controller/HelloWorldController.php namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Response; class HelloWorldController { public function helloWorldAction() { return new Response( '<html><body>Hello World!</body></html>' ); } }

Twig

HTMLHTML。。SymfonyTwig。

TwigSymfonyController

// src/AppBundle/Controller/HelloWorldController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; class HelloWorldController extends Controller { public function helloWorldAction() { $text = 'Hello World!'; return $this->render('hello-world.html.twig', ['text' => $text]); } }

https://riptutorial.com/zh-TW/home 5

Twigapp/Resources/views/hello-world.html.twig

<html><body>{{ text }}</body></html>

Twig{{ text }}text。HTML

<html><body>Hello World!</body></html>

404

404。 SymfonyNotFoundHttpException 。

SymfonycreateNotFoundException

public function indexAction() { // retrieve the object from database $product = ...; if (!$product) { throw $this->createNotFoundException('The product does not exist'); } // continue with the normal flow if no exception is thrown return $this->render(...); }

Request

RequestHTTP

use Symfony\Component\HttpFoundation\Request; public function indexAction(Request $request) { $queryParam = $request->query->get('param'); // ... }

Symfonyrequest。

https://riptutorial.com/zh-TW/symfony/topic/10085/

https://riptutorial.com/zh-TW/home 6

4:

SymfonyRequestHTTP。URLcookie。

$ - > getPathInfo; //URL。https://example.com/foo/bar?key=value / foo / bar•$ - > - >''; //$ _GET•$ request-> query-> get'id'1; //•$ - > - >''; //$ _POST•$ - > - >''; //“attachment”UploadedFile•$ - > cookies->'PHPSESSID'; //cookie$ _COOKIE•$ - >> GET“CONTENT_TYPE”; //HTTP•$ - > getMethod; //HTTPGETPOSTPUTDELETE•$ - > getLanguages; //•

Examples

。3

http://example.com/products?page=3

HTTP

GET /products?page=3 HTTP/1.1 Host: example.com Accept: text/html User-Agent: Mozilla/5.0 (Macintosh)

query

$page = $request->query->get('page'); // 3

page

$page = $request->query->get('page', 1);

http://example.com/products $page1 。

Request

PHPHTTP $_POST $_GET $_FILES $_SESSIONRequestcreateFromGlobals()

use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals();

Symfony。app.php / app_dev.php。 。

https://riptutorial.com/zh-TW/home 8

POST

method="post"post

$name = $request->request->get('name');

cookie

$id = $request->cookies->get('PHPSESSID');

“PHPSESSID”cookie。

https://riptutorial.com/zh-TW/symfony/topic/10097/

https://riptutorial.com/zh-TW/home 9

5:

URL 。 Symfony。

RoutingYAMLXMLPHP。

。 book_show

。 /book/{isbn}

Examples

YAML

# app/config/routing.yml blog_list: path: /blog defaults: { _controller: AppBundle:Blog:list }

// src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog", name="blog_list") */ public function listAction() { // ... } }

/blog URLlistAction()BlogControllerAppBundle 。

YAML

# app/config/routing.yml blog_show: path: /blog/{slug} defaults: { _controller: AppBundle:Blog:show }

// src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller;

https://riptutorial.com/zh-TW/home 10

use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog/{slug}", name="blog_show") */ public function showAction($slug) { // ... } }

URL/blog/*showAction()BlogControllerAppBundle 。。

/blog/my-postshowAction()$slugmy-post 。slug my-post。

YAML

# app/config/routing.yml blog_list: path: /blog/{page} defaults: { _controller: AppBundle:Blog:list, page: 1 } requirements: page: '\d+'

// src/AppBundle/Controller/BlogController.php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; class BlogController extends Controller { /** * @Route("/blog/{page}", name="blog_list", requirements={"page": "\d+"}) */ public function listAction($page = 1) { // ... } }

/blog/blog/1 URLblog_listlistAction()。/blog listAction()$page1 。

https://riptutorial.com/zh-TW/symfony/topic/10084/

https://riptutorial.com/zh-TW/home 11