0

I've been trying to find a example of this or a tutorial without having to resort to deconstructing a module off Magento connect, but I can't seem to find an example of doing this.

I've created a module that allows users in the back end to create lookbooks. They can tick them of as active/inactive, upload images, assign products to the image add a name and a archive image. This all works fine, and I'm able to access the model just fine in my templates header to output a list of all active lookbooks.

However I'm absolutely stuck when it comes to adding a custom route for say www.example.com/lookbook/{lookbook_id} or better yet www.example.com/lookbook/{lookbook_slug} so a user can view a specific lookbook. I've read several articles that talk about making a blog module, however they all seem to stop short of making the single pages for these modules, they seem to just have index pages.

I've been through the Magento extension developers guide and know it will involve adding a frontend routers in my config.xml but without an example, tutorial or better idea of what this is called in Magento terms I'm stuck.

Edit

I've managed to get the route working, and the view method is getting the correct data as i'm able to log it. However I'm not getting anything on page. With template hints on it's loading the 3 col template not the specified one col and the content is't being brought out.

This is my frontend/base/default/layout/lookbook.xml file

<?xml version="1.0"?>
 <layout version="0.1.0">

<lookbook_lookbook_view>
    <update handle="page_one_column" />
    <reference name="content">
        <block type="lookbook/lookbook_view" name="lookbook_view" template="lookbook/view.phtml" />
        </reference>
    </lookbook_lookbook_view>

</layout>

Exit 2

Idiot as I am, Completely forgot to add my layout file to my XML, so I've added the following to my config.xml

    <frontend>
        ...
        <layout>
            <updates>
                <Ahoy_Lookbook>
                    <file>lookbook.xml</file>
                 </Ahoy_Lookbook>
            </updates>
        </layout>
        ...
    </frontend>
</config> 

So it's now loading the one page col correctly. Just doesn't seem to want to add my block to content.

2

It's not clear to me, but I assume you don't want to use the url module/controller/action/id/{id_here}.
If you do...then this works out of the box. You just have to create a viewAction method in your lookbooks controller and get the id from the request with $this->getRequest()->getParam('id'). Then go on from there. Load your model instance put it in Mage::register(), then read the value you set in a block that uses a template to render.

But even if you don't want your url to look like that you still have to do it.

If you want to use a custom url like /lookbook/{id} you will need a custom router that reads url and if it looks like /lookbook/{id} then it does an internal redirect to the module/controller/action/id/{id} (the one mentioned above).

Here is an example on how you can do this custom router.

[EDIT]
you will need an action in your controller and a method that checks the validity of your lookbook.
Something like this:

protected function _initLookbook()
{
    ${{entity}}Id   = $this->getRequest()->getParam('id', 0);
    ${{entity}}     = Mage::getModel('lookbook/lookbook') //adjust this according to your settings
        ->load($lookbookId);
    if (!$lookbook->getId()) {
        return false;
    } elseif (!$lookbook->getStatus()) { //or getIsActive() or however your status field is named
        return false;
    }
    return $lookbook;
}

public function viewAction()
{
    $lookbook = $this->_init{{Entity}}();
    if (!$lookbook) { //if entity does not exist or not enabled go to 404 page
        $this->_forward('no-route');
        return;
    }
    Mage::register('current_lookbook', $lookbook);
    $this->loadLayout();
    $this->renderLayout();
}

Then create a block. [Namespace]/[Module]/Block/Lookbook/View.php

<?php
class [Namespace]_[Module]_Block_Lookbook_View extends Mage_Core_Block_Template
{

    public function getCurrentLookbook()
    {
        return Mage::registry('current_lookbook');
    }
}

now the template app/design/frontend/base/default/template/lookbook/view.phtml

<?php $_lookbook = $this->getCurrentLookbook();?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<div class="page-title lookbook-title">
    <h1><?php echo $_lookbook->getName(); ?></h1> <!-- or getTitle or what ever you need to show -->
</div>
<div class="lookbook-view">
    <!-- your html goes here -->
</div>

now in the layout file of your module add this:

<[module]_[controller]_view>
    <update handle="page_two_columns_left" /><!-- can be any other layout -->
    <reference name="content">
        <block type="lookbook/lookbook_view" name="lookbook_view" template="lookbook/view.phtml" />
    </reference>
</[module]_[controller]_view>

Make sure you adjust all the values between [] to fit your module's convention. Clear the cache and give it a go.
You should see the entity details at the url [module]/[controller]/view/id/{id}.
If you want a custom route, use the example in the link I mentioned above.

  • I updated my question with better URL's but yes that seems to be what I was after. Though I'm still a little lost on how to actually populate said page with content and specify templates etc – Chris Morris Aug 12 '15 at 14:55
  • See my update. I hope it helps. – Marius Aug 12 '15 at 15:21
  • Thanks so much, with this and a hell of a lot of Googeling it's almost working. Everything seems to work but the layout update. Despite being set to page_one_column in my update the front end is trying to load 3columns.phtml and the content isn't being updated. – Chris Morris Aug 13 '15 at 16:50
  • The view.php needed to capitalised to View.php. After chanigng that it all worked. – Chris Morris Aug 14 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.