symfony2 mvc model view
play

- Symfony2 MVC Model - PowerPoint PPT Presentation

- Symfony2 MVC Model View Controller


  1. ُاگشًادص یتعٌ فیرش یسذٌْه ُذکشًاد رتَیپهاک یسانشراک سرد راهب۱۳۹۱ - ۹۲ تمسق۹ Symfony2 MVC وی همانرب

  2. Model ِهاًرب قطٌه View Controller یاّ ُداد راذیاپ View شیاوً Model Controller شیاوً ٍ Model یزاساذج 2

  3. Symfony2 کرٍ نیرف اّ ًِاخباتک اّراسبا اّدراذًاتسا Web Framework MVC رب یٌتبه 3

  4. Symfony2 ی ِهاًرب رب یٌتبهکرٍ نیرف Bundle اّ یا ِعَوجه زا BlogBundle AdminBundle StatisticsBundle MVC کیی ِهاًرب لهاک Model اّ View اّ Controller اّ تاویظٌت ... 4

  5. اّ تًٌاپهاک HttpFoundation HttpKernel Config Form Security Routing Validator … 5

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

  7. Request سلبک use Symfony\Component\HttpFoundation\R equest; $request = Request::createFromGlobals(); $request->getPathInfo(); 7

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

  9. app/ راتخاساّ لیاف cache/ config/ logs/ src/ ... vendor/ ... web/ app.php 9

  10. Front Controller Application Kernel یاّ لیاف کیتاتسا تاویظٌت کرتشه عباٌه ٍ Bundle اّ Cache Logs 10

  11. Bundle ذیذج کی يتخاس php app/console generate:bundle --namespace=CE/BlogBundle --format=yml 11

  12. Controller اّ تاویظٌت Routing ) یبایریسه ( اّ سیٍرس Template اّ یاّ لیاف یاتسیا یهَوع اّ ِوجرت اّ تست 12

  13. YAML تهرف --- - name: Ali country: Mohammadi age: 24 - name: Pedraam country: IR 13

  14. Page Controller /list.php /blog.php?id=12 List View Post View Post Controller List Controller Model/Data Source 14

  15. Front Controller /frontend.php/blog?id=12 /frontend.php/blog/12 /blog/12 List View Post View Front Controller Post Controller List Controller Model/Data Source 15

  16. Front Controller Routing Controller Action View 16

  17. 17

  18. Controller Action Controller Action Action Action 18

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

  20. Response فیرعت $content = $this->renderView( 'BlogBundle:Blog:list.html.twig', []); $response = new Response($content); return $response; ىاٌَع ِبربًایه render عبات return $this->render( 'BlogBundle:Blog:list.html.twig', []); 20

  21. یاّ طیحه فلتخه dev prod test // app/AppKernel.php $loader->load( تاویظٌت __DIR__.'/config/config_'. $this->getEnvironment(). '.yml ‘ ); { 21

  22. یّد ماً URL فیرعت post_show: path: /post/{id} defaults: { _controller: BlogBundle:Blog:post} app رد اّریسه Bundle اّ رد اّریسه // app/config/routing.yml blog: resource: "@BlogBundle/Resources/config/routing.yml" 22

  23. سردآرلرتٌک post_show: path: /post/{id} defaults: اّرتهاراپ _controller: BlogBundle:Blog:post اّ ماً color: green ىاٌَع ِبىاهَگرآ Request public function postAction(Request $request) } $post = getPost($request->query->get('id'); } 23

  24. ىدرک redirect return $this->redirect( $this->generateUrl('homepage') ); return new RedirectResponse( $this->generateUrl('homepage') ); return $this->redirect( $this->generateUrl('homepage'), 301 ); 24

  25. forward ىدرک request لاعف ىٍرد Client ىذش ِجَته ىٍذب $response = $this->forward( ‘ BlogBundle:Hello:fancy', [ 'name' => $name, 'color' => 'green‘ ] ( ; return $response; 25

  26. اّ سیٍرس Dependency Injection $httpKernel = $this->container->get('http_kernel'); $templating = $this->get('templating'); $doctrine= $this->get( ‘doctrine' ); $doctrine= $this->getDoctrine(); 26

  27. یاّ ماغیپ اطخ Exception throw $this->createNotFoundException( 'The blog post does not exist'); throw new \Exception('Something went wrong!'); 27

  28. Response $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, ] ); 28

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

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

  31. Flash یاّ ماغیپ یذعب تساَخرد رد $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 %} 31

  32. Symfony Book, 2.2. 32

Recommend


More recommend