404 Page Not Found With CodeIgniter REST_Controller - rest

I'm trying to use CodeIgniter REST_Controller. Following is my code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once (APPPATH.'/libraries/REST_Controller.php');
class Test extends REST_Controller{
public function index_get() {
$this->response(array(''));
}
}
When I access http://codeigniter/index.php/test the response
<xml><item/></xml>
is shown on the browser.
But when trying to access
http://codeigniter/index.php/test.xml or http://codeigniter/index.php/test.html I get a 404 error.
The documentation of the above mentioned library says that both
http://example.com/books.json
http://example.com/books?format=json
works with the library.
Can someone point out to me what I'm doing wrong?

You must call it like this
SERVER-URL/index.php/test/index
And if you have the test.php inside a folder in controllers then call it like this
SERVER-URL/index.php/test/FOLDER-NAME/index
Tahir Rauf
http://www.onethatmatters.com

Related

Calling module controller function from page formulary in CodeIgniter 4.1 HMVC

I'm working in an application HMVC CodeIgniter 4.1, all the routes work fine in navigator:
Navigator adress: http://localhost/myapp/public/index.php/installation/shop-data
My module Routes file contains:
$routes->group("installation", ["namespace" => "\Modules\Installation\Controllers"], function ($routes) {
//Example URL: /installation/shop-data
$routes->get("shop-data", "InstallationController::shop_data");
$routes->get("shop-data-post", "InstallationController::shop_data_post");
});
shop_data_post is the function in which I insert the data to the database from the page Form using this:
echo form_open("installation/shop-data-post");
But I obtain this 404 error:
Controller or its method is not found: \App\Controllers\Installation::shop-data-post
How can I get my function correctly from my page formulary?
Thanks
Ok finally I have solved this..
All that I must did was change my module Installation Routes file so:
This line:
$routes->get("shop-data-post", "InstallationController::shop_data_post");
For this:
$routes->match(["get", "post"], "shop-data-post", "InstallationController::shop_data_post");
The error was on the route request method. It must be POST for process the Form data

Sitemap for dynamic website - codeigniter

I have a dynamic website designed with Codeigniter 3 and I am working on the sitemap part as a newbie.
I found the library sitemap-php from evert/sitemap-php but I can't make it run.
From now this is what I did, I put the Sitemap.php file into my library folder
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Deals extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url', 'form', 'security');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('email');
$this->load->model('deal_model');
$this->load->helper(array('cookie','custom','text'));
}
public function Sitemap(){
$this->load->library('Sitemap');
$sitemap = new Sitemap('https://www.mywebsite.com');
$sitemap->setPath('/public_html/Sitemap/'); // I created a folder Sitemap into my public folder
$sitemap->setFilename('sitemap');
$sitemap->addItem('/', '1.0', 'daily', 'Today');
$sitemap->createSitemapIndex('https://www.mywebsite.com/sitemap/', 'Today');
}
Then when I go to https://www.mywebsite.com/sitemap/, I have an Error 404.
Could you guide me to solve my issue.
Thanks
The docs for that library describe that it generates a static XML file. The code you've shown will do that - but your code is in a Library, and you have not run it yet. You need to run it, then it will generate an XML file as you've specified, in /public_html/Sitemap/. From your description you are looking for the XML before doing anything to generate it, and it does not (yet) exist.
From your updated code, you now have the code to generate the static XML available as a Controller method. According to the standard Codeigniter routing conventions, the method you have created is accessible at:
http://your-site/deals/Sitemap
(Maybe you've also set up some routes so it is accessible at other URIs also.)
Visit that URL, once, to generate the static XML file at /public_html/Sitemap/sitemap.xml. Assuming your code works, you should then be able to browse the XML at
http://your-site/Sitemap/sitemap.xml
Side note: AFAIK Codeigniter convention is for capitalised Controller file and class names (Deal.php and Deal), but all lower-case method names (sitemap() instead of Sitemap()). You can see examples of this in the Controller docs I linked above. I am not sure if it matters, just pointing it out.

CodeIgniter REST_Controller POST method returning GET

I'm using REST_Controller CodeIgniter library in my project.
I have a login post method that cannot being reachable because REST_Controller is setting method as GET even if I do a POST.
Here is a snippet of my Auth.php controller:
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/core/REST_Controller.php';
class Auth extends REST_Controller
{
function __construct()
{
parent::__construct();
}
public function login_get()
{
echo('get');
}
public function login_post()
{
echo('post');
}
}
When I do a POST at http://localhost/auth/login using RESTED Google Chrome extension I'm getting the echo('get').
Debbuging REST_Controller I could see that the function _detect_method() is returning method as GET, and this is because in the function method() of Input core class of CodeIgniter $this->server('REQUEST_METHOD') is returning GET.
Why is this happening?
Nevermind, I got the solution!
This was happening just because I forgot to enable mod_rewrite in my Apache.
This fixes the problem:
sudo a2enmod rewrite
sudo service apache2 restart

Importing CURL into block in Magento 2

I have a custom block that needs to request data from an external API.
Examples refer to a construct like this:
class CustomerBalance extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
)
But that breaks the site and the page fails to load correctly without logging any errors.
I have the same problem trying to use GuzzleHttp when I import that.
I guess there is something I'm missing about Magento 2 importing...
Or is there another way to access the Magento curl object?
Any help would be appreciated.
It appears I was simply importing the wrong class. This works:
protected $_httpAdapter;
public function __construct(
\Magento\Framework\HTTP\Adapter\Curl $httpAdapter
){
$this->_httpAdapter = $httpAdapter;
}
A more complete answer can be found here:
https://magento.stackexchange.com/questions/153993/magento-2-curl-send-header-with-response

Zend Framework website.com/username

One of the application I am developing using Zend Framework requires the user's profile page to be accessed via website.com/username, while other pages should be accessed by website.com/controller_name/action_name
I am not too sure how can this be achieved, however, I feel this can be done with some tweaks in the .htaccess file.
Can someone here please help me out?
Many thanks in advance
As suggested before, you can use a custom route that will route single level requests. However, this will also override the default route. If you're using modules, this will no longer work example.com/<module>.
I have done this before but only for static pages. I wanted this:
example.com/about
instead of this:
example.com/<some-id>/about
while maintaining the default route so this still works
example.com/<module>
example.com/<controller>
The way I did this was using a plugin to test if my request could be dispatched. If the request could not be dispatched using the default route, then I would change the request to the proper module to load my page. Here is a sample plugin:
class My_Controller_Plugin_UsernameRoute extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
if (!$dispatcher->isDispatchable($request)) {
$username = $request->getControllerName();
$request->setModuleName('users');
$request->setControllerName('dashboard');
$request->setActionName('index');
$request->setParam('username', $username);
/** Prevents infinite loop if you make a mistake in the new request **/
if ($dispatcher->isDispatchable($request)) {
$request->setDispatched(false);
}
}
}
}
What about using Zend_Controller_Router_Route, look here the link http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard.variable-requirements