blog ب zendframework 2 الجزء الرابع

imadbelasri Zend
ZN

فهاد الجزء الرابع من blog ب zendframework 2 غادي نكملو الملفات لي غادي يكونوا فيهم  les forms لي غادي يمكنوا المستخدم من إضافة تعديل أو حدف article كما غادي نشوفوا الملف لي غادي يكون هو الصفحة الرئيسية ديالنا ولي غادي نعرضوا فيه les articles ديالنا مرفوقين ب les catégories. 


نظرة سريعة بالفيديو


1- الملف CategoryController.php

فالمجلد  module/Post/src/Post/Controller كنزيد ملف جديد سميه CategoryController.php ولي فيه les fonctions لي غادي نحتاجو فعندي indexAction لي كترجع les categories لي عندي فالجدول category وكترسلهم للملف index ولي غادي نزيدوه من بعد ثم كاين addAction لي كتمكن من إضافة category للجدول category من بعد مكتدير ل validation للفورم منبعد كاين viewAction كترجعنا categorie باستعمال ل id ديالها بالإضافة ل les articles ديالها ولي كنصيفطوهم للملف view ولي غادي يتزاد من بعد ثم كاين editAction لي كترجع categorie بل id ديالها ومنبعد كتديرلها modification منبعد مكتدير ل validation للفورم ثم كاين deleteAction لي كتمسح categorie باستعمال ل id ديالها منبعد عندي getPostTable لي كترجعنا la table post ونفس الشي بالنسبة ل la table categories الكود ديال الملف هو :

                                                    
                                                        <?php
namespace Post\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Post\Form\CategoryForm;  
use Post\Model\Category; 
use Post\Model\Post; 

class CategoryController extends AbstractActionController
{   
    protected $postTable;
    protected $categoryTable;

    public function indexAction()
    {
        return new ViewModel(array(
             'categories' => $this->getCategoryTable()->fetchAll(),
        ));
    }
    public function viewAction(){
        //get the id from the url
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('category', array(
                'action' => 'index'
            ));
        }
        return new ViewModel(array(
            'posts' => $this->getPostTable()->getCategoryArticles($id),
            'categorie' => $this->getCategoryTable()->getCategory($id)
        ));  
    }
    public function addAction()
    {

        $form = new CategoryForm();
        $form->get('submit')->setValue('Valider');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $categorie = new Category();
            $form->setInputFilter($categorie->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $categorie->exchangeArray($form->getData());
                $this->getCategoryTable()->saveCategory($categorie);
                return $this->redirect()->toRoute('category');
            }
        }
        return array('form' => $form);
    }

    public function editAction()
    {
        //get the id from the url
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toRoute('category', array(
                'action' => 'index'
            ));
        }
        // Get the Category with the specified id.  An exception is thrown
        // if it cannot be found, in which case go to the index page.
        try {
            $categorie = $this->getCategoryTable()->getCategory($id);
        }
        catch (\Exception $ex) {
            return $this->redirect()->toRoute('category', array(
                'action' => 'index'
            ));
        }

        $form  = new CategoryForm();
        $form->bind($categorie);
        $form->get('submit')->setAttribute('value', 'Modifier');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setInputFilter($categorie->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $this->getCategoryTable()->saveCategory($categorie);

                // Redirect to list of categories
                return $this->redirect()->toRoute('category');
            }
        }
        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    public function deleteAction()
    {
         //get the id from the url
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('category');
         }
         $request = $this->getRequest();
         if ($request->isPost()) {
             //check if the user say yes or no
             $del = $request->getPost('del', 'No');
             if ($del == 'Yes') {
                 $id = (int) $request->getPost('id');
                 $this->getCategoryTable()->deleteCategory($id);
             }
             // Redirect to list of categories
             return $this->redirect()->toRoute('category');
         }
         return array(
             'id'    => $id,
             'categorie' => $this->getCategoryTable()->getCategory($id)
         );
    }
    public function getPostTable()
    {
        if (!$this->postTable) {
            $sm = $this->getServiceLocator();
            $this->postTable = $sm->get('Post\Model\PostTable');
        }
        return $this->postTable;
    }
    public function getCategoryTable()
    {
        if (!$this->categoryTable) {
            $sm = $this->getServiceLocator();
            $this->categoryTable = $sm->get('Post\Model\CategoryTable');
        }
        return $this->categoryTable;
    }
}
                                                    
                                                

2- الملف home.phtml

فالمجلد module/Post/view/post/post كنزيد ملف جديد كنسميه home.phtml  فيه كيتعرضوا les articles وles categories لي عندي ولي غادي يكون هو الصفحة الرئيسية ديالنا فكنستقبلهم من ل fonction homeAction وكنخدم ب foreach باش كنعرض المعلومات فبلايصهم وعندي الروابط لي كنعرض بهم article اختارو المستخدم و les articles الخاصين ب catégorie معينة الكود ديال الملف هو :

                                                        
                                                            <?php
 $title = 'Zend Blog';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <hr>
 <div class="container">
    <div class="row">
        <div class="col-md-8">
            <?php foreach ($posts as $post) : ?>
                <h4 class="text-info"><?php echo $this->escapeHtml($post->title);?></h4>
                <small class="text-danger"><?php echo $this->escapeHtml($post->created);?></small>
                <p class="lead"><?php echo $this->escapeHtml(substr($post->body,0,100));?>...</p>
                <a href="<?php echo $this->url('post',
                    array('action'=>'view', 'id' => $post->id));?>" class="text-warning">Voir</a>
                <hr>
            <?php endforeach; ?>
        </div>  
        <div class="col-md-4">
            <div class="panel panel-primary">
               <h4 class="text-primary" style="padding:5px;margin-left:8px;">Catégories</h4>
               <ul class="list-group">
                    <?php foreach ($categories as $categorie) : ?>
                        <li class="list-group-item"><a href="<?php echo $this->url('category',
                array('action'=>'view', 'id' => $categorie->id));?>" class="text-warning"><?php echo $this->escapeHtml($categorie->name);?></a></li>
                    <?php endforeach; ?>
               </ul>
            </div>  
        </div>  
    </div>  
</div>   
    
   
                                                        
                                                    

3- الملف index.phtml

دائما فالمجلد module/Post/view/post/post كنزيد ملف جديد كنسميه index.phtml  فيه كيتعرضوا les articles لي كيجيو من ل fonction indexAction فجدول مع الروابط لي كتدي للملفات الخاصين بمشاهدة تعديل أوحدف article وأيضا الرابط لي كيدي للملف الخاص بإضافة article الكود ديال الملف هو :

                                                        
                                                            <?php
 $title = 'Articles';
 $this->headTitle($title);
 ?>
 <h1><?php echo $this->escapeHtml($title); ?></h1>
 <hr>
 <p class="pull-right">
     <a href="<?php echo $this->url('post', array('action'=>'add'));?>" class="btn btn-primary">Ajouter</a>
 </p>
 <table class="table">
    <tr>
        <th>Titre</th>
        <th>Description</th>
        <th>Action</th>
    </tr>
    <?php foreach ($posts as $post) : ?>
    <tr>
        <td><?php echo $this->escapeHtml($post->title);?></td>
        <td><?php echo $this->escapeHtml($post->body);?></td>
        <td>
            <a href="<?php echo $this->url('post',
                array('action'=>'view', 'id' => $post->id));?>" class="text-info">Voir</a>
            <a href="<?php echo $this->url('post',
                array('action'=>'edit', 'id' => $post->id));?>" class="text-warning">Modifier</a>
            <a href="<?php echo $this->url('post',
                array('action'=>'delete', 'id' => $post->id));?>" class="text-danger">Supprimer</a>
        </td>
    </tr>
    <?php endforeach; ?>
 </table> 
                                                        
                                                    

4- الملف add.phtml

دائما فالمجلد module/Post/view/post/post كنزيد ملف جديد كنسميه add.phtml فيه كاين الفورم لي كتمكن من إدخال المعلومات الخاصة ب article ولي كيمشيو لل fonction addAction لي كاينة ف PostController الكود ديال الملف هو :

                                                        
                                                            <?php
 $title = 'Ajouter un article';
 $this->headTitle($title);
 ?>
<div class="container">
    <div class="row">
       <div class="col-md-6 col-md-offset-3">
       <h3 class="text-info"><?php echo $this->escapeHtml($title); ?></h3>
       <hr>
        <?php
            echo '<div class="row">';
                echo '<div class="col-md-12">';
                    $form->setAttribute('action', $this->url('post', array('action' => 'add')));
                    $form->prepare();
                    echo $this->form()->openTag($form);
                    echo $this->formHidden($form->get('id'));
                    echo '<div class="form-group">'.$this->formRow($form->get('category_id')).'</div>';
                    echo '<div class="form-group">'.$this->formRow($form->get('title')).'</div>';
                    echo '<div class="form-group">'.$this->formRow($form->get('body')).'</div>';
                    echo '<div class="form-group">'.$this->formSubmit($form->get('submit')).'</div>';
                    echo $this->form()->closeTag();
                echo '</div>';
            echo '</div>'
        ?>
    </div> 
  </div> 
</div>
                                                        
                                                    

5- الملف edit.phtml

دائما فالمجلد module/Post/view/post/post كنزيد ملف جديد كنسميه edit.phtml فيه كاين الفورم لي كتمكن من تعديل المعلومات الخاصة ب article ولي كيمشيو لل fonction editAction لي كاينة ف PostController فمنبعد مكيضغط المستخدم على modifier كيتوجه للملف لي فيه الفورم ولي فيها كيتعرض ال article لي اختار الكود ديال الملف هو :

                                                        
                                                            <?php

 $title = 'Modifier un article';
 $this->headTitle($title);
 ?>
 <div class="container">
    <div class="row">
       <div class="col-md-6 col-md-offset-3">
       <h3 class="text-info"><?php echo $this->escapeHtml($title); ?></h3>
       <hr>
        <?php
            echo '<div class="row">';
                echo '<div class="col-md-12">';
                    $form = $this->form;
                    $form->setAttribute('action', $this->url(
                        'post',
                        array(
                            'action' => 'edit',
                            'id'     => $this->id,
                        )
                    ));
                    $form->prepare();
                    echo $this->form()->openTag($form);
                    echo $this->formHidden($form->get('id'));
                    echo '<div class="form-group">'.$this->formRow($form->get('category_id')).'</div>';
                    echo '<div class="form-group">'.$this->formRow($form->get('title')).'</div>';
                    echo '<div class="form-group">'.$this->formRow($form->get('body')).'</div>';
                    echo '<div class="form-group">'.$this->formSubmit($form->get('submit')).'</div>';
                    echo $this->form()->closeTag();
                echo '</div>';
            echo '</div>'
        ?>
    </div> 
  </div> 
</div>