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