0

I'm using Magento 1.8.1.0 community edition.

I have managed to add a custom admin controller and the default indexAction() method does work. However, if I try to access the other method, anotherAction() I get redirected to the Admin dashboard.

Swapping the methods' names works, so I don't think there is any issue with the code in the other method and caching has been turned off.

The 2 URLs in question are:

I did a little digging and discovered that the request to the /another action is met with a 302 redirection to the admin dashboard.

Any pointers on what the issue could be?

Basically I'm trying to add a new controller with 2 actions as a new admin page. I do not want to change / override the current functionality.

Thanks in advance!

<?

class Printo_CategoryAttribute_IndexController extends Mage_Adminhtml_Controller_Action
{
    private $filepath = 'var/import/latest_categories.csv'; // location of file

    public function indexAction()
    {   
        $category = Mage::getModel ('catalog/category');
        $tree = $category->getTreeModel ();
        $tree->load ();
        $ids = $tree->getCollection()->getAllIds();

        if ($ids)
    {
       mkdir('var/import');
       file_put_contents($this->filepath,"Category ID, Category Name" . PHP_EOL);

           foreach ( $ids as $id )
           {
               $string = $id . ', ' .$category->load($id)->getName() . PHP_EOL;
               file_put_contents($this->filepath,$string,FILE_APPEND);
            }

           $x = print_r(get_class_methods($this),TRUE);
       $this->loadLayout();
           //create a text block with the name of "printo-block"
           $block = $this->getLayout()
           ->createBlock('core/text', 'printo-block')
           ->setText("<h1><a href ='/category_info/index/download'>Download</a></h1><h2><pre>$x</pre></h2>");

           $this->_setActiveMenu('printo_menu/category_info');  

           $this->_addContent($block);
           $this->renderLayout();
        }
    }


    public function downloadAction()
    {
/*            
           $this->getResponse()
                    ->setHttpResponseCode(200)
                    ->setHeader('Content-Type', 'text/csv', TRUE)
                    ->setHeader('Content-Length', filesize($this->filepath), TRUE)
                    ->setHeader('Content-Disposition', 'attachment; filename="latest_categories.csv"', TRUE);
           $this->getResponse()->clearBody();
           $this->getResponse()->sendHeaders();
           readfile($this->filepath);

           $this->loadLayout();
           //create a text block with the name of "printo-block"
           $block = $this->getLayout()
           ->createBlock('core/text', 'printo-block')
           ->setText("<h1>YAY IT WORKS!</h1>");
           $this->_setActiveMenu('printo_menu/category_info');
           $this->_addContent($block);
           $this->renderLayout();
*/
       $fileName ='Category Info.csv';
       $this->_prepareDownloadResponse($filename, file_get_contents($this->filepath));
     }

}


?>

Here is the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Printo_CategoryAttribute>
            <version>0.0.1</version>
        </Printo_CategoryAttribute>
    </modules>

    <admin>
        <routers>
            <Printo_CategoryAttribute_Admin_Page>
                <use>admin</use>
                <args>
                    <module>Printo_CategoryAttribute</module>
                    <frontName>category_info</frontName>
                </args>
            </Printo_CategoryAttribute_Admin_Page>
        </routers>
    </admin>

   <adminhtml>
        <menu>
            <printo_menu translate="title" module="CategoryAttribute">
                <title>Printo</title> 
                <sort_order>5000</sort_order>
                <children>
                    <category_info module="CategoryAttribute">
                        <title>Category Info</title> 
                        <action>category_info/index/index</action>
                    </category_info>
                </children>
            </printo_menu>
        </menu> 
    </adminhtml>

    <global>

    <helpers>
        <CategoryAttribute>
            <class>Printo_CategoryAttribute_Helper</class>
        </CategoryAttribute>
    </helpers>

    <resources>
            <add_category_attribute>
                <setup>
        <module>Printo_CategoryAttribute</module>
        <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute>
            <add_category_attribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_write>
            <add_category_attribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_read>
    </resources>
    </global>

</config>

2 Answers 2

1

I figured out what the issue was. You need to use:

$url = $this->getUrl('category_info/index/download');

to get the full url including the key used to authenticate the current logged-in session.

The result of this is that, you get a link like so

http://dev.printo.in/index.php/category_info/index/download/key/4745379c10d9a5fa01bb4bb4645b59bc/

Earlier I was just using category_info/index/download and that wouldn't work.

0
0

If you add this custom admin controller

Using Rewrite Admincontroller:

then it should be

SiteUrl/admin/index/index

Whenever rewrite like

<admin>
<routers>
    <adminhtml>
    <args>
        <modules>
        <Printo_CategoryAttribute before="Mage_Adminhtml">Printo_CategoryAttribute</Printo_CategoryAttribute>
        </modules>
    </args>
    </adminhtml>
</routers>
</admin>

According to SiteanmeUrl/admin/controllername/ActionName

.See at

Using basic admin url define:

If http://dev.printo.in/index.php/category_info/index/index work whenever admin route config

like

<admin>
<routers>
    <customadmin>
    <use>admin</use>
    <args>
        <module>Printo_CategoryAttribute</module>
        <frontName>category_info</frontName>
    </args>
    </category_info>
</routers>
</admin>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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