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.