Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Magento Development 07: Collection and Block

    Now that we have saved, let’s create a list where customer can view all of his/her blogs.

    Redirection and Messages

    Earlier we displayed just a simple message saying “Saved” when customer submitted form. But it will be better to redirect to the list page after saving the data.

    So we have to edit the Controller/Manage/Save.php file,

    It’s a good practice to declare the member variables. So we have declared them at the stating of the class. To redirect we have used resultRedirectFactory and called setPath method with the url at which we want to redirect. As you may have noticed here we have given only two parts of the url that’s because it will autofill the missing part with “index“. So our url will become blog/manage/index.

    Now let’s talk about \Magento\Framework\Message\ManagerInterface class. This is use to show messages on the page. There are four types of messages available in magento.
    i) Success
    ii) Error
    iii) Notice
    iv) Warning

    Also notice that in the addSuccess method we have passed message enclosed in __(“”) . All static text should be enclosed within __(‘some text’) so that it become translatable. Magento allows us to create translation for different languages with the help of .csv file.

    Now let’s create the index action in Contoller/Manage/Index.php which will be used to show the blogs list.


    Here we will be showing a phtml file so we need to create layout file as view/frontend/layout/blogmanager_manage_index.xml

    Here everything should be familiar except the block class. Earlier we used the default block class for the Blog Add page but for the Blog List page we will create a Block class Webkul\BlogManager\Block\BlogList.

    Why we need block? The need of block is to pass data to the template file. The block class contains the logical part of the view. All the data needed by the template file should come from the block. We do not write complex code in the phtml file, instead we write the complex codes in the block and pass the result to the phtml file.

    Now let’s create the Block folder under the module directory. In the Block folder we need to create the block file BlogList.php as

    All Blocks must extend the default block class \Magento\Framework\View\Element\Template. We will use this block to load the blogs data. Note here we are using the collection \Webkul\BlogManager\Model\ResourceModel\Blog\CollectionFactory because we need to load multiple rows from the table. Just like the model the model collection also has the factory class.

    Here we have getBlogs() method in which we are loading the collection and returning the whole collection. Just like model factory here also we need call the create() method to create a new object of the collection.

    Now that we have created the block let’s create the list.phtml file for this block under view/frontend/templates folder.

    The <?= ?> is nothing but a shorthand notation for <?php echo ?>. You can find about it in the php manual. I have already told about the __(“”), it’s used for translation.

    Now see the important line $blogs = $block->getBlogs(); here we are calling the getBlogs() function of the block which will return the blogs collection. Remember I told you that collection is collection of models, so when we loop over the collection with a foreach loop then we will get a model in each iteration. That means in each iteration we will get a row from the table.

    The $blog->getId() will return the entity_id that means it acts as getEntityId() that’s because in the model we have written this method,

    Just like the setters that we saw in the previous blog the getters get internally managed, we just have to use the camelCase naming convention for each column. So we can use getTitle(), getContent() to access the title and content column respectively, even though we have not written these methods in the model file.

    Now when you submit the add blog form you will be redirected to blog list page with a success message like,

    Let’s create a customer navigation link for this page also, we have to edit view/frontend/layout/customer_account.xml

    Here in the block node we have added before=”customer-account-navigation-blog-add” so it will add the blog list menu before the blog add menu. You will be able to see the Blogs link in the customer navigation,

    PS. We can get the collection from the model class also. When we call the $model->getCollection() it returns the collection of the model.

    PS. Just like setData the model and collections have getData, which returns the data in associative array format. So during development or debugging when we have to view data, we use echo ‘<pre>; print_r($collection->getData()); die; which prints out the data in nice viewable format.

    Folder structure till now,

    Next Blog -> Magento Development 08: Filter and Sorting

    Previous Blog -> Magento Development 06: Form Validation and Saving Data

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again