Restler 404 error Routes.php:431 with xampp - php

I am trying to set up a development environment on Windows with xampp 5.6.23.
I have an API I want to set up with Restler 3.0.0 so that I can browse to http://localhost/api/cars/search?term=red to call the search($term) function in the C:\xampp\htdocs\api\cars.php file:
<?php
class cars{
public function search($term) {
// do search...
return $result;
}
}
I also have a C:\xampp\htdocs\api\index.php set up with:
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/luracast/restler/vendor/restler.php';
use Luracast\Restler\Restler;
Defaults::$crossOriginResourceSharing = true;
$r = new Luracast\Restler\Restler();
$r->setCompatibilityMode('2');
$r->setSupportedFormats('JsonFormat', 'XmlFormat', 'JsFormat');
Luracast\Restler\Format\JsonFormat::$unEscapedUnicode = false;
$r->addAPIClass('cars');
$r->addAPIClass('Luracast\\Restler\\Resources');
$r->handle(); //serve the response
and C:\xampp\htdocs\api\.htdocs:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors On
</IfModule>
But if I go to any URL (/ or /cars) I get a 404 error:
<response>
<error>
<code>404</code>
<message>Not Found</message>
</error>
<debug>
<source>Routes.php:431 at route stage</source>
<stages>
<success>get</success>
<failure>route</failure>
<failure>negotiate</failure>
<failure>message</failure>
</stages>
</debug>
</response>
I have tried multiple answers from SO but none have worked in my instance. I have uncommented LoadModule rewrite_module modules/mod_rewrite.so and set AllowOverride All everywhere I could find it in httpd.conf. I have also tried moving my everything to htdocs instead of a sub folder but still get the same result.

Since you don't have an index() function but only a search() function in your class cars, you need to access the URL /cars/search/term.
Or are you trying to achieve something else?

Related

Deploy PHP Slim project to Hostgator using subdomain

I have a very simple Slim app that works fine on my Ubuntu machine but I get an error 500 when I deploy it to a Hostgator shared account using a subdomain.
I suspect the problem lies in my .htaccess file.
The structure of my files is:
project/
├──api/
│ ├── .htaccess
│ ├── services.php
├──src/
├──vendor/
My Slim app (services.php):
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require_once '../src/vendor/autoload.php';
$c = new \Slim\Container();
$c['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
return $c['response']
->withStatus(404)
->withHeader('Content-Type', 'text/html')
->write('Invalid endpoint');
};
};
$app = new \Slim\App($c);
$app->post('/myEndpoint', function (Request $request, Response $response, array $args) {
if($request->getHeader("key") != null && $request->getHeader("key")[0] == "123456789="){
$reqdata = $request->getParsedBody();
$data = $reqdata['data'];
return json_encode('{"data":'.$data.',"response":"OK","errorcode":"0"}');
} else {
return '{"data":"","response":"Access denied","errorcode":"9999"}';
}
});
$app->run();
My .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . services.php [QSA,L]
</IfModule>
# Use PHP71 as default
AddHandler application/x-httpd-php71 .php
<IfModule mod_suphp.c>
suPHP_ConfigPath /opt/php71/lib
</IfModule>
I created a subdomain (api.mydomain.com) with project/api/ as document root.
When I make a POST request in my local installation to
127.0.0.1/api/services.php/myEndpoint
works as expected and returns the data. However, when I make the same request to my Hostgator subdomain:
api.mydomain.com/services.php/myEndpoint
I get the error 500.
Note that if I add a simple test.php file to my api/ directory that only prints something, e.g.
<?php
echo "OK"
?>
and I go to api.mydomain.com/test.php, it works fine and prints the string.
What do I need to change or add in my .htaccess for my slim app to work?
I solved it by doing two things:
I moved my project folder to public_html, created again the subdomain and pointed it to the project directory.
I updated my .htaccess file IfModule mod_rewrite.c section. It now looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ services.php [QSA,L]
</IfModule>
The difference with the previous one is I added this line:
RewriteBase /
And this one:
RewriteRule . services.php [QSA,L]
updated to this:
RewriteRule ^ services.php [QSA,L]
I got the .htaccess fixes here:
https://discourse.slimframework.com/t/deploy-slim-project-to-hostgator-using-subdomain/3187/7

Controller function not found on the server in codeigniter

So I got exactly this 404 error :
Not Found
The requested URL /adminigniter1/Usercontroller/insert was not found on this server.
Apache/2.4.18 (Ubuntu) Server at localhost Port 80
When I wanted to call a function from the controller, from what I read about it has to do with the .htaccess file(and I placed that in my source folder adminigniter1), but mine seems to look ok:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /adminigniter1/index.php
</IfModule>
From my index.php I send to that controller function like this:
<form method = "POST" action = "<?php echo base_url('Usercontroller/insert') ?>">
And this is my controller :
<?php
class Usercontroller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Usermodel');
}
public function index() {
$data["content"]= "user/index";
$data["getStatus"] = $this->Usermodel->getStatus();
$this->load->view("main",$data);
}
public function insert() {
$datai= $this->input->post();
if(isset($datai)){
echo $datai['txtApartament'];
exit;
}
}
}
What can be wrong ?
Ok, I found the reason for that, I will post it here since some one can have this problem. All the issues were on apache2 side:
If you use ubuntu go :
sudo gedit /etc/apache2/apache2.conf
There you will have to edit field like this :
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
In my case AllowOverride was set to None, so you will have to set it to All, another thing, check the root for your localhost /var/www/html/ in my case, to be ok!

My class autoload function is not working in my mvc

MVC uploaded on cloud9, but when the auto-load class located in lib/init.php is not working. The .htaccess in the project folder or webroot folder files are not properly configured.
When I visit my project link https://my-mvc-hunteelar.c9users.io/ it gives this exception :
Failed to load class: Config' in /home/ubuntu/workspace/lib/init.php on line 21
I have 2 .htaccess files. The first .htaccess code in the main project folder is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
The second .htaccess file located in the webroot folder which is supposed to be the public folder of my MVC:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
The init.php code which contains the __autoload function.
require_once (ROOT.DS.'config'.DS.'config.php');
function __autoload($class_name){
$lib_path = ROOT.DS.'lib'.DS.strtolower($class_name).'class.php';
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
$models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';
if(file_exists($lib_path)){
require ($lib_path);
}
elseif (file_exists($controllers_path)){
require_once ($controllers_path);
}
elseif (file_exists($models_path)){
require_once ($models_path);
}
else{
throw new Exception('Failed to load class : '.$class_name);
}
}
Here is my code uploaded on cloud9 https://ide.c9.io/hunteelar/my-mvc
I think you already solved this. But the problem is with the initialization of your code.
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
should actually be:
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'.controller.php';
Also
$models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';
Should be
$model_path = ROOT.DS.'models'.DS.strtolower($class_name).'.php';
In case it may help someone else.

Symfony 2 project in nested directory

I need to deploy a Symfony 2 project in nested directory on production server. Effectively, this would mean that all URLs are prefixed with /subdirectory/ path, i.e.
http://host.com/subdirectory/project/web/app.php/survey
I don't need URL rewriting and are am not going to set it up. The app should work when accessed via above URL only.
The problem I have is that all links generated by path and asset Twig functions are relative to server root (/), and not subdirectory the project is in (/subdirectory/). Is there any config parameter in Symfony 2 to override relative paths globally?
I tried to work around that problem by adding HTML tag, but it doesn't work for links.
Update: I owe you further details. I'm running IIS with its version of mod_rewrite, so part of your suggestions may still be valid.
Update: Ideally I would like to see a solution that sets root dir on AppKernel object - method AppKernel::setRootDir() had it existed to complement existing AppKernel::getRootDir().
Strange... with default config, path/asset should handle subdirectories. Have you tried it with symfony distribution out-of-the-box? On localhost im mostly using it this way.
Would you mind posting your config file?
Anyway...
For assets paths you could try to configure:
#config.yml
templating:
assets_base_urls: { http: "http://yoursite.com/dir1", ssl: "https://yoursite.com/dir1" }
(http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls)
For router you should read: http://symfony.com/doc/current/cookbook/console/sending_emails.html#configuring-the-request-context-globally
It could give you some ideas about changing router context like:
# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: http
router.request_context.base_url: my/path
Anyway - context is automaticlly set based on request information. Try to debug:
$context = $this->getContainer()->get('router')->getContext();
// you should be mainly interested in:
$context->getBaseUrl();
It is possible to easily create listener class which will manually set your base url if anyhow it is wrong:
class RouterContextListener {
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelRequest(Event $event)
{
$this->router->setBaseUrl('somedir');
}
}
You are looking for RewriteBase rule, add it to your in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/project/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
Also take a look onto symfony-standard 2.3 .htaccess file, mb it can help
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
Look at app/config/routing.yml where you import routing from your bundles. Records in that file would look similar to this:
somename:
resource: "#YourBundle/Resources/config/routing.yml"
prefix: /
You can add your subdirectory in prefix, like prefix: /subdirectory
Then all generated URLs will include subdirectory.
To enable it the rewrite module, run "apache2 enable module rewrite":
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart
VirtualHost should be like this
ServerName localhost.myproject.dev
ServerAdmin webmaster#localhost
DocumentRoot /var/www/project_path
<Directory /var/www/project_path>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</Directory>
this option is redirect your index.php, index.html or whatever:
Options Indexes FollowSymLinks MultiViews
it will rewrite your htacces!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
My solution to this on a local environment was adding the following code in app/config/config.yml
framework:
assets:
base_urls:
- 'http://localhost/symfony_dir/'
No rewrite needed!

Restler php returning 404 for all results

I've been looking into implementing a REST implementation for my API and came across Restler. I've installed Restler 2.0 and am using Apache and php > 5.3. My class is below:
Class Sample
{
function gettest()
{
return "This is a test";
}
}
and the index.php is:
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/restler/');
require_once('path/to/Sample.class.php');
spl_autoload_register('spl_autoload');
$r = new Restler();
$r->setSupportedFormats('JsonFormat');
$r->addAPIClass('Sample');
$r->handle();
and instead of using an .htaccess, I've included the following inside a
<Directory /path/to/REST_DIR>
AllowOverride All
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Order deny,allow
Allow from all
</Directory>
in my httpd.conf file (since it is supposed to be faster if you have access to edit it).
Whenever I try the following path:
http://www.myhost.com/REST_DIR/test/
I get:
{
"error": {
"code": 404,
"message": "Not Found"
}
}
Is there something I'm missing or some way to more thoroughly debug Restler to determine why it can't find the function gettest()?
Thanks in advance.
R
as per the current configuration you can expect result from http://www.myhost.com/REST_DIR/sample/test
If you want the url to be http://www.myhost.com/REST_DIR/test instead, change index.php as follows
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/restler/');
require_once('path/to/Sample.class.php');
spl_autoload_register('spl_autoload');
$r = new Restler();
$r->setSupportedFormats('JsonFormat'); //not needed JSON is the default
$r->addAPIClass('Sample',''); // note the empty string
$r->handle();

Categories