ساخت وبلاگ در symfony https://drupalweb.ir/ fa ایجاد controller در symfony https://drupalweb.ir/blog/controller-symfony <span class="field field--name-title field--type-string field--label-hidden">ایجاد controller در symfony</span> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">حامد حسینی</span></span> <span class="field field--name-created field--type-created field--label-hidden">س., 03/31/2020 - 16:08</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>میخواهیم یک کنترلر در symfony ایجاد کنیم و در نهایت خروجی json داشته باشیم :</p> <p>بعد از نصب symfony و استارت سرور در پوشه src در پوشه Controller یک فایل به اسم BlogController ایجلد میکنیم </p> <pre> <code class="language-php">class BlogController extends AbstractController {}</code></pre> <p>در قسمت بعد میخواهیم 3 صفحه داشته باشیم که تمامی پست ها و یک پست سرچ یک پست را ببینیم </p> <p>به همین ترتیب 3 تابع به همین نام ها ایجاد ایجاد میکنیم</p> <pre> <code class="language-php"> public function list() {} public function post() {} public function postBYSlug() {}</code></pre> <p>برای roure در symfony  ما به صورت حاشیه نویسی و از Annotation  استفاده میکنیم که میتوانید با استفاده از دستور زیر آن را نصب کیند </p> <pre> <code class="language-bash">composer require annotations</code></pre> <p>ما در اینجا میخواهیم پست های وبلاگ خودمون را از یک تابع که به صورت private تعیرف کرده ایم بگیریم که به صورت زیر است </p> <pre> <code class="language-php"> private const POST = [ [ 'id' =&gt; 1, 'slug' =&gt; 'hello world', 'title' =&gt; 'hello world' ], [ 'id' =&gt; 2, 'slug' =&gt; 'one hello world', 'title' =&gt; 'one hello world' ], [ 'id' =&gt; 3, 'slug' =&gt; 'two hello world', 'title' =&gt; 'two hello world' ], ];</code></pre> <p>که در تابع اول میخواهیم تمامی پست ها رو ببینیم </p> <pre> <code class="language-php"> /** * @Route("/{page}", name="blog_list ", defaults={"page": 5 }) */ public function list($page = 1) { return new JsonResponse( [ 'page' =&gt; $page, 'data' =&gt; array_map(function ($item){ return $this-&gt;generateUrl('blog_by_id', ['id' =&gt; $item['id']]); } ,self::POST) ] ); }</code></pre> <p>در تابع دوم یک پست را با استفاده از id </p> <pre> <code class="language-php"> /** * @Route("/post/{id}", name="blog_id ") */ public function post($id) { return new JsonResponse( self::POST[ array_search($id, array_column(self::POST, 'id'))] ); }</code></pre> <p>و در تابع بعدی سرچ یک پست با استفاده از یک مقدار متغییر </p> <pre> <code class="language-php"> /** * @Route("/post/{slug}", name="blog_by_slug ") */ public function postBYSlug($slug) { return new JsonResponse( self::POST[array_search($slug, array_column(self::POST, 'slug'))] ); }</code></pre> <p>که در نهاییت کد ما به شکل زیر می باشد </p> <pre> <code class="language-php">&lt;?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; /** * Class BlogController * @Route("/blog") */ class BlogController extends AbstractController { private const POST = [ [ 'id' =&gt; 1, 'slug' =&gt; 'hello world', 'title' =&gt; 'hello world' ], [ 'id' =&gt; 2, 'slug' =&gt; 'one hello world', 'title' =&gt; 'one hello world' ], [ 'id' =&gt; 3, 'slug' =&gt; 'two hello world', 'title' =&gt; 'two hello world' ], ]; /** * @Route("/{page}", name="blog_list ", defaults={"page": 5 }) */ public function list($page = 1) { return new JsonResponse( [ 'page' =&gt; $page, 'data' =&gt; array_map(function ($item){ return $this-&gt;generateUrl('blog_by_id', ['id' =&gt; $item['id']]); } ,self::POST) ] ); } /** * @Route("/post/{id}", name="blog_by_id", requirements={"id"="\d+"}) */ public function post($id) { return new JsonResponse( self::POST[ array_search($id, array_column(self::POST, 'id'))] ); } /** * @Route("/post/{slug}", name="blog_by_slug ") */ public function postBYSlug($slug) { return new JsonResponse( self::POST[array_search($slug, array_column(self::POST, 'slug'))] ); } }</code></pre> <p> </p> </div> <div class="field field--name-field-image field--type-image field--label-hidden field__item"> <img loading="lazy" src="/sites/default/files/styles/blog/public/1399-01/symfony-controller.jpg?itok=9wzFmg8M" width="700" height="300" alt="ایجاد controller در symfony" title="ایجاد controller در symfony" typeof="foaf:Image" class="image-style-blog" /> </div> <div class="field field--name-field-tags field--type-entity-reference field--label-hidden clearfix"> <ul class='links field__items'> <li><a href="/brchsbha/sakht-wblag-dr-symfony" hreflang="fa">ساخت وبلاگ در symfony</a></li> </ul> </div> <div class="field field--name-field-dsth-bndy field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/dsth-bndy-wblag/symfony" hreflang="fa">symfony</a></div> </div> <section class="field field--name-field-comments field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'>افزودن دیدگاه جدید</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=347&amp;2=field_comments&amp;3=comment" token="IKY8vq1Bp1xBuPTGz3VswkNSMykH43kmPNlFet9NL7I"></drupal-render-placeholder> </section> Tue, 31 Mar 2020 11:38:20 +0000 حامد حسینی 347 at https://drupalweb.ir https://drupalweb.ir/blog/controller-symfony#comments