- Symfony2 MVC Model - - PowerPoint PPT Presentation

symfony2 mvc model view
SMART_READER_LITE
LIVE PREVIEW

- Symfony2 MVC Model - - PowerPoint PPT Presentation

- Symfony2 MVC Model View Controller


slide-1
SLIDE 1

یسانشراک سرد راهب۱۳۹۱-۹۲

ُاگشًادص یتعٌ فیرش یسذٌْه ُذکشًاد رتَیپهاک

تمسق۹ MVC وی همانرب Symfony2

slide-2
SLIDE 2

Model

ِهاًرب قطٌه یاّ ُداد راذیاپ

View

شیاوً

Controller یزاساذج Model شیاوً ٍ 2

View Model Controller

slide-3
SLIDE 3

کرٍ نیرف Symfony2

اّ ًِاخباتک اّراسبا اّدراذًاتسا Web Framework رب یٌتبه MVC

3

slide-4
SLIDE 4

ی ِهاًرب رب یٌتبهکرٍ نیرف Symfony2 یا ِعَوجه زا Bundleاّ

BlogBundle AdminBundle StatisticsBundle

کیی ِهاًرب لهاک MVC

Modelاّ Viewاّ Controllerاّ تاویظٌت ...

4

slide-5
SLIDE 5

اّ تًٌاپهاک

HttpFoundation HttpKernel Config Form Security Routing Validator …

5

slide-6
SLIDE 6

Composer

6

> composer init { "require": { "php": ">=5.3.3", "symfony/http-foundation": "2.2.* " { { > composer install

slide-7
SLIDE 7

سلبک Request

7

use Symfony\Component\HttpFoundation\R equest; $request = Request::createFromGlobals(); $request->getPathInfo();

slide-8
SLIDE 8

Composer

8

composer create-project symfony/framework-standard-edition . 2.2.1

slide-9
SLIDE 9

راتخاساّ لیاف

9

app/ cache/ config/ logs/ src/ ... vendor/ ... web/ app.php

slide-10
SLIDE 10

Front Controller

Application Kernel یاّ لیاف کیتاتسا

تاویظٌت کرتشه عباٌه ٍ Bundleاّ Cache Logs

10

slide-11
SLIDE 11

کی يتخاس Bundle ذیذج

11

php app/console generate:bundle

  • -namespace=CE/BlogBundle
  • -format=yml
slide-12
SLIDE 12

Controllerاّ تاویظٌت

یبایریسه ( Routing ) اّ سیٍرس

Templateاّ یاّ لیاف یاتسیا یهَوع اّ ِوجرت اّ تست

12

slide-13
SLIDE 13

تهرف YAML

13

  • name: Ali

country: Mohammadi age: 24

  • name: Pedraam

country: IR

slide-14
SLIDE 14

Page Controller

14

/list.php /blog.php?id=12

Post Controller List Controller Model/Data Source Post View List View

slide-15
SLIDE 15

Front Controller

15

/frontend.php/blog?id=12 /frontend.php/blog/12 /blog/12

Post Controller List Controller Model/Data Source Post View List View Front Controller

slide-16
SLIDE 16

View

16

Front Controller Controller Action Routing

slide-17
SLIDE 17

17

slide-18
SLIDE 18

Controller Action

18

Controller Action Action Action

slide-19
SLIDE 19

19

class BlogController extends Controller } public function listAction() } $posts = getAllPosts(); } public function postAction($id) } $post = getPost($id); } {

slide-20
SLIDE 20

20

$content = $this->renderView( 'BlogBundle:Blog:list.html.twig', []); $response = new Response($content); return $response; return $this->render( 'BlogBundle:Blog:list.html.twig', []);

فیرعت Response عبات render ىاٌَع ِبربًایه

slide-21
SLIDE 21

یاّ طیحه فلتخه

dev prod test

تاویظٌت

21

// app/AppKernel.php $loader->load( __DIR__.'/config/config_'. $this->getEnvironment(). '.yml‘); {

slide-22
SLIDE 22

یّد ماً فیرعت URL رد اّریسه app رد اّریسه Bundleاّ

22

post_show: path: /post/{id} defaults: { _controller: BlogBundle:Blog:post}

// app/config/routing.yml blog: resource: "@BlogBundle/Resources/config/routing.yml"

slide-23
SLIDE 23

سردآرلرتٌک اّرتهاراپ

اّ ماً

Request ىاٌَع ِبىاهَگرآ

23

post_show: path: /post/{id} defaults: _controller: BlogBundle:Blog:post color: green public function postAction(Request $request) } $post = getPost($request->query->get('id'); }

slide-24
SLIDE 24

redirect ىدرک

24

return $this->redirect( $this->generateUrl('homepage') ); return new RedirectResponse( $this->generateUrl('homepage') ); return $this->redirect( $this->generateUrl('homepage'), 301 );

slide-25
SLIDE 25

forward ىدرک

ىٍرد requestلاعف ىذش ِجَته ىٍذب Client

25

$response = $this->forward( ‘BlogBundle:Hello:fancy', [ 'name' => $name, 'color' => 'green‘] (; return $response;

slide-26
SLIDE 26

اّ سیٍرس

Dependency Injection

26

$httpKernel = $this->container->get('http_kernel'); $templating = $this->get('templating'); $doctrine= $this->get(‘doctrine'); $doctrine= $this->getDoctrine();

slide-27
SLIDE 27

یاّ ماغیپ اطخ

Exception

27

throw $this->createNotFoundException( 'The blog post does not exist'); throw new \Exception('Something went wrong!');

slide-28
SLIDE 28

Response

28

$response = new Response('Hello '.$name, 200); $response = new Response( json_encode(['name' => $name])); $response->headers->set('Content-Type', 'application/json'); $response = new JSONResponse( ['name' => $name, ] );

slide-29
SLIDE 29

Request

29

$request = $this->getRequest(); $request->isXmlHttpRequest(); $request->getPreferredLanguage(['en', 'fa']); $request->query->get('page'); $request->request->get('page');

slide-30
SLIDE 30

Session

30

$session = $this->getRequest()->getSession(); $session->set('name', 'Mohammad'); $foo = $session->get('name'); $filters = $session->get('name', "Anonymous");

slide-31
SLIDE 31

یاّ ماغیپ Flash

یذعب تساَخرد رد

31

$this->get('session')->getFlashBag()-> add('notice', 'Your changes were saved!');

{% for flashMessage in app.session.flashbag.get('notice') %} <div class="flash-notice"> {{ flashMessage }} </div> {% endfor %}

slide-32
SLIDE 32

Symfony Book, 2.2.

32