1

I have module named Spaarg_Api. and I have controller Controller/Customers/Index.php and it is working fine, but when I try to create another file in Customer directory like Controller/Customers/List.php it gives 404.

Here is my route.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="api" frontName="api">
            <module name="Spaarg_Api" />
        </route>
    </router>
</config>

Here is my controllers

Customers/Index.php

<?php
/**
 *
 * Copyright © 2015 Spaargcommerce. All rights reserved.
 */
namespace Spaarg\Api\Controller\Customers;

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\StateInterface $cacheState,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
        $storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
        $currentStore = $storeManager->getStore();
        $baseUrl = $currentStore->getBaseUrl();

        $token = $this->getRequest()->getPost('token');
        $email = $this->getRequest()->getPost('email');
        $password = $this->getRequest()->getPost('password');
        $contact_number = $this->getRequest()->getPost('contact_number');

        $addresses = array();
        $addresses['id'] = '0';
        $addresses['customer_id'] = '0';
        $addresses['region']['region_code'] = 'CA';
        $addresses['region']['region'] = 'California';
        $addresses['region']['region_id'] = '12';
        $addresses['region']['extension_attributes'] = '';
        $addresses['region_id'] = '12';
        $addresses['country_id'] = 'US';
        $addresses['street'][] = 'Zoe Ave';
        $addresses['company'] = 'None';
        $addresses['fax'] = 'None';
        $addresses['postcode'] = '90001';
        $addresses['city'] = 'Los Angeles';
        $addresses['firstname'] = 'None';
        $addresses['lastname'] = 'None';
        $addresses['middlename'] = 'None';
        $addresses['prefix'] = 'None';
        $addresses['suffix'] = 'None';
        $addresses['vat_id'] = 'None';
        $addresses['default_shipping'] = true;
        $addresses['default_billing'] = true;
        $addresses['extension_attributes'] = '';
        $addresses['telephone'] = $contact_number;

        $postArray = array();
        $postArray['customer']['id'] = '0';
        $postArray['customer']['group_id'] = '0';
        $postArray['customer']['default_billing'] = 'None';
        $postArray['customer']['default_shipping'] = 'None';
        $postArray['customer']['confirmation'] = 'None';
        $postArray['customer']['created_at'] = date('Y-m-d H:i:s');
        $postArray['customer']['updated_at'] = date('Y-m-d H:i:s');
        $postArray['customer']['created_in'] = date('Y-m-d H:i:s');
        $postArray['customer']['dob'] = date('Y-m-d H:i:s');
        $postArray['customer']['email'] = $email;
        $postArray['customer']['firstname'] = 'eMenu';
        $postArray['customer']['lastname'] = 'Customer';
        $postArray['customer']['middlename'] = '';
        $postArray['customer']['prefix'] = 'None';
        $postArray['customer']['suffix'] = 'None';
        $postArray['customer']['gender'] = '1';
        $postArray['customer']['store_id'] = '1';
        $postArray['customer']['taxvat'] = 'None';
        $postArray['customer']['website_id'] = '1';
        $postArray['customer']['addresses'][] = $addresses;
        $postArray['customer']['disable_auto_group_change'] = '0';
        $postArray['customer']['extension_attributes']['is_subscribed'] = true;
        $postArray['password'] = $password;
        $postArray['redirectUrl'] = '0';

        $ch = curl_init($baseUrl."rest/V1/customers");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postArray));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
        $result = curl_exec($ch);
        $result = (array)json_decode($result,TRUE);
        $resultArray = array();
        if(!empty($result['id'])){

            $resultArray['customer']['id'] = $result['id'];
            $resultArray['customer']['email'] = $result['email'];
            $resultArray['customer']['contact_number'] = $result['addresses'][0]['telephone'];
            $resultArray['error'] = '200';
            $resultArray['message'] = 'You are successfully registered.';
        }
        else{
            $resultArray['error'] = '400';
            $resultArray['message'] = !empty($result['message'])?$result['message']:'Bad request.';
        }
        echo json_encode($resultArray); 
    }

    public function details()
    {
        echo "Hello there...!";
    }


}

Customers/List.php

<?php
/**
 *
 * Copyright © 2015 Spaargcommerce. All rights reserved.
 */
namespace Spaarg\Api\Controller\Customers;

class List extends \Magento\Framework\App\Action\Action
{

    public function execute()
    {
        echo "Hiii There, you calling me...?";exit;
    }

}

It will be great if someone can help.

2
  • Which Url you call for the second controller? Commented Dec 16, 2016 at 16:28
  • Thanks for your quick reply, I am calling [store_url]/api/customers/index it is working perfect, but [store_url]/api/customers/list is giving 404
    – Anand
    Commented Dec 16, 2016 at 16:30

1 Answer 1

2

Name your file ListAction.php and your class ListAction.
It does not work with List because it is a reserved php word.
For all (declared) reserved php words, M2 appends Action to the name and then looks for the file. For the same reason all the new actions map to the file NewAction.php.
Here is an example of the cms page new action controller.
The file name is /Magento/Cms/Controller/Adminhtml/Page/NewAction.php but the url is admin/cms/page/new.
You can find a list of all reserved words that will get Action at the end of the file name in lib/internal/Magento/Framework/App/Router/ActionList.php.
And in the same file in the method get you will see action being appended to the file name

4
  • What is a reserved php word? You can explain more? Commented Dec 16, 2016 at 16:35
  • Thanks man ! worked for me, it was a great help. I think it's magento structure that controller other then default must be followed by Action
    – Anand
    Commented Dec 16, 2016 at 16:37
  • 1
    I edited my answer. But here is some context outside Magento for php reserved words nusphere.com/kb/phpmanual/reserved.htm
    – Marius
    Commented Dec 16, 2016 at 16:38
  • Great info @Marius
    – Anand
    Commented Dec 16, 2016 at 16:38

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.