ُاگشًادص یتعٌ فیرش یسذٌْه ُذکشًاد رتَیپهاک یسانشراک سرد راهب۱۳۹۱ - ۹۲ تمسق۹ Symfony2 MVC وی همانرب
Model ِهاًرب قطٌه View Controller یاّ ُداد راذیاپ View شیاوً Model Controller شیاوً ٍ Model یزاساذج 2
Symfony2 کرٍ نیرف اّ ًِاخباتک اّراسبا اّدراذًاتسا Web Framework MVC رب یٌتبه 3
Symfony2 ی ِهاًرب رب یٌتبهکرٍ نیرف Bundle اّ یا ِعَوجه زا BlogBundle AdminBundle StatisticsBundle MVC کیی ِهاًرب لهاک Model اّ View اّ Controller اّ تاویظٌت ... 4
اّ تًٌاپهاک HttpFoundation HttpKernel Config Form Security Routing Validator … 5
Composer > composer init { "require": { "php": ">=5.3.3", "symfony/http-foundation": "2.2.* " { { > composer install 6
Request سلبک use Symfony\Component\HttpFoundation\R equest; $request = Request::createFromGlobals(); $request->getPathInfo(); 7
Composer composer create-project symfony/framework-standard-edition . 2.2.1 8
app/ راتخاساّ لیاف cache/ config/ logs/ src/ ... vendor/ ... web/ app.php 9
Front Controller Application Kernel یاّ لیاف کیتاتسا تاویظٌت کرتشه عباٌه ٍ Bundle اّ Cache Logs 10
Bundle ذیذج کی يتخاس php app/console generate:bundle --namespace=CE/BlogBundle --format=yml 11
Controller اّ تاویظٌت Routing ) یبایریسه ( اّ سیٍرس Template اّ یاّ لیاف یاتسیا یهَوع اّ ِوجرت اّ تست 12
YAML تهرف --- - name: Ali country: Mohammadi age: 24 - name: Pedraam country: IR 13
Page Controller /list.php /blog.php?id=12 List View Post View Post Controller List Controller Model/Data Source 14
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
Front Controller Routing Controller Action View 16
17
Controller Action Controller Action Action Action 18
class BlogController extends Controller } public function listAction() } $posts = getAllPosts(); } public function postAction($id) } $post = getPost($id); } { 19
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
یاّ طیحه فلتخه dev prod test // app/AppKernel.php $loader->load( تاویظٌت __DIR__.'/config/config_'. $this->getEnvironment(). '.yml ‘ ); { 21
یّد ماً 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
سردآرلرتٌک 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
ىدرک redirect return $this->redirect( $this->generateUrl('homepage') ); return new RedirectResponse( $this->generateUrl('homepage') ); return $this->redirect( $this->generateUrl('homepage'), 301 ); 24
forward ىدرک request لاعف ىٍرد Client ىذش ِجَته ىٍذب $response = $this->forward( ‘ BlogBundle:Hello:fancy', [ 'name' => $name, 'color' => 'green‘ ] ( ; return $response; 25
اّ سیٍرس Dependency Injection $httpKernel = $this->container->get('http_kernel'); $templating = $this->get('templating'); $doctrine= $this->get( ‘doctrine' ); $doctrine= $this->getDoctrine(); 26
یاّ ماغیپ اطخ Exception throw $this->createNotFoundException( 'The blog post does not exist'); throw new \Exception('Something went wrong!'); 27
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
Request $request = $this->getRequest(); $request->isXmlHttpRequest(); $request->getPreferredLanguage(['en', 'fa']); $request->query->get('page'); $request->request->get('page'); 29
Session $session = $this->getRequest()->getSession(); $session->set('name', 'Mohammad'); $foo = $session->get('name'); $filters = $session->get('name', "Anonymous"); 30
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
Symfony Book, 2.2. 32
Recommend
More recommend