I've been trying to figure this out for hours, but haven't been able to get anywhere. There are so many questions asked in stackoverflow, and I've tried almost all of them, but still haven't been able to figure out what the problem is that I'm having.
I'm working on a project for my Master's thesis for which I have to perform some data analysis. I'm building a site using php, backbone, and mongodb. I'm using xampp, and this is my directory structure:
htdocs
|-MyProject
|------API
|---Slim
|---index.php
|------scripts
|-------App
|----Collections
|----Models
|----Views
|-------lib
|-index.html
The index.html is the boilerplate html stuff and calls backbone collections and views. The index.php within the directory API instantiates slim and has GET method.
Here's the index.php
<?php
echo 'test';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
use Slim\Slim;
$app = new Slim();
$app->get('/trends', 'getTrends');
and my backbone collections
App.Collections.TrendsCollection = Backbone.Collection.extend({
model : App.Models.TrendModel,
url : "API/trends",
initialize: function(){
console.log('collections');
}
});
I read that I may need to set RewriteRule, so I tried this:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /API/index.php [R,NC]
but this didn't find the method I'm trying to reach either. I've been trying to figure this out for too long, and I'm pressing for time. Can someone please give me some guidance?
EDIT:
function getTrends(){
echo 'Hello';
}
I think your problem is in getTrends function. Show me that function.
After hours of trying to figure it out, I finally realized that the rewrite rules should be in .htaccess in the same directory as index.php. It finally worked.
Related
I am attempting to implement an oembed provider using the Silverstripe framework but have come across an issue.
I have a controller routed from the url /omebed.json and it works fine if I call something like /omebed.json?mediaurl=mymovie.mp4.
However the Oembed standard states it should be /omebed.json?url=mymovie.mp4
But Silverstripe internally checks the $_GET['url'] variable and will attempt to route to that page/controller.
So SilverStripe is trying to route to /mymovie.mp4 skipping my controller and hitting the ErrorPage_Controller creating a 404.
I'm thinking im going to have to extend the ErrorPage_Controller and rejig it if the url is oembed.json, but this seems a little hackish.
Any suggestions?
Cheers
Extending on #Stephen's answer, here is a way to get around that issue without duplicating main.php and without modifying it directly.
What I did was create a _ss_environment.php file which is added early on in the loading process of Silverstripe.
_ss_environment.php
global $url;
$url = $_GET['raw_url'];
if (isset($_GET['url']))
{
unset($_GET['url']);
}
// IIS includes get variables in url
$i = strpos($url, '?');
if($i !== false)
{
$url = substr($url, 0, $i);
}
.htaccess
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule .* framework/main.php?raw_url=%1 [QSA]
So here is what is happening:
The .htaccess is now using raw_url instead of url
_ss_environment.php is being called early in the loading process, setting the global $url variable that main.php normally sets. This is set with raw_url rather than url.
To prevent main.php to just override it again when it sees your url query string parameter, it is unset (Silverstripe seems to reset this later as far as my test is concerned).
Lastly is a little block of code that main.php would normally run if $_GET['url'] is set, copied as-is for apparent support in IIS. (If you don't use IIS, you likely won't need it.)
This has a few benefits:
No update to main.php allows upgrading Silverstripe slightly easier in the future
Runs the minimal amount of code needed to "trick" Silverstripe into thinking it is running normally.
The one obvious drawback to any solution for changing away form the url query string parameter is if anything looks at the parameter directly. With how Silverstripe works, it is more likely that code uses the $url global variable or the Director class rather than looking at the query string for the current URL.
I tested this on a 3.1 site by doing the changes I mentioned and:
Creating a controller called TestController
In the init function of the controller, I am running the following:
var_dump($_GET['url']);
var_dump($this->getRequest()->getVars());
Visited /TestController?url=abc123, saw the value of both dumps have "abc123" as the value for the URL parameter.
Navigated to a few other custom pages on the site to make sure they were still working (no issues that I saw)
Unfortunately, I haven't been able to find documentation for the order of inclusion in regards to _config.php and _ss_environment.php. However, after browsing through the code, I have worked out it is this:
main.php runs, first main task is to require core/Constants.php
Constants.php's first task is to search for _ss_environment.php in the base folder and potential parent folders. If it finds it, it will be included.
Going back to main.php (and after the $_GET['url'] check is done in main.php), it will start an ErrorControlChain which it internally does another require for core/Core.php
Inside Core.php, it performs calls for the config manifest
ConfigManifest.php exposes the functions to actually add _config.php files and for them to be required.
I could probably go on however I think this gives a pretty good picture of what is going on. I don't really see a way around not using the _ss_environment.php file. Nothing else gets included early enough that you can hook into without modifying core code.
I had a quick play with this the other day. And looking at what main.php does it might be best to hack away at it rather than ErrorPage_controller.
For startes SS's default .htaccess file does this:
<IfModule mod_rewrite.c>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# RewriteBase /silverstripe
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1&%{QUERY_STRING} [L]
</IfModule>
Note the ?url changing that to something else and then changing main.php's usage as well may/should help or will cause a heap of extra errors and sadness.
To avoid hacking the core/framework, you could change the .htaccess to target a copy of main.php in mysite (with appropriate include changes).
I am creating a project with Code Igniter as a back end framework and Bootstrap 3 as a front end framework.
I'm having an issue with accessing my pages via directly calling the controller followed by the method.
For example my controller is site.php and the method is home.
Here is what is looks like.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function index(){
$this->home();
}
public function home(){
$data["title"] ="SmartAgent";
$this->load->view("site_header");
$this->load->view("content_home", $data);
$this->load->view("site_footer");
}
As I understand the method index basically sets the method home as the index page.
When I type the web address in my url such as:
examplesite.co.uk
The controller correctly loads my view for the home method, which is content_home.php and the site loads the homepage along with the title fine.
However if I type:
examplesite.co.uk/site/home
This does not work! And I do not know why, this is further causing me issues such as URL's not working etc etc. However base url is set, and I can load CSS, JS, and image files fine. Also I have enabled helpers, routes and all else.
The above url works to load another project I was working on. So is why I know I'm missing something.
Any ideas anyone?
Thanks
Codeigniter routing is done relative to the index.php.
So your link should be examplesite.co.uk/index.php/site/home.
If that is the issue, then you need an .htaccess file, and in it write
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
(If I am not mistaken, writing from my phone)
Then, you will remove index.php from your site.
With codeigniter, you need to set $route[]. This is in application/config/routes.php
Also check out the codeigniter documentation on this, its pretty good and will explain it all.
Thank you everyone for your help and answers!
This was a silly mistake of mine and I wanted to let everyone know so that if anyone was to face this issue this may help. #Alexey answer above gave me a light bulb moment! So thank you.
Basically within the .htaccess mod rewrite file which can be downloaded from google. I forgot to change the directory for my server which is located at the top on line 4
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourfolderdirectoryhere/
If you are unsure or unclear please watch below tutorial which helped me.
https://www.youtube.com/watch?v=dynPx1B0jis
I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!
As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.
I've been successfully running Slim apps on a couple different servers and tried setting one up on AppFog today using the same structure, but it isn't running normally.
I'll start with my directory structure:
.htaccess
/public
.htaccess
index.php
/routes
/Slim
The root .htaccess file contains the DocumentRoot code from the AppFog docs.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^brs.aws.af.cm$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.brs.aws.af.cm$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
The /public directory is where my api code will go, and the Slim index.php and .htaccess files currently are. The index.php file contains two simple routes:
require '../Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// Default GET route
$app->get('/', function () {
echo "Default GET route";
});
// Hello World route
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
The server is setup at http://brs.aws.af.cm/ and I've listed the main routes below:
/ => uses the default GET route
/hello/john => 404 Error
/public/hello/john => works, but requires "/public" in the url
And here's some extra weirdness. Seven-character routes result in a 404 error, six or less end up using the default GET route.
/123456 => shouldn't work, but uses the default GET route
/1234567 => 404 error
I'm completely stumped. I figure it has something to do with the DocumentRoot code, but I'm not sure what exactly. I've also tried setting
RewriteBase /public/
in /public/.htaccess but it doesn't seem to affect anything.
Any help would be greatly appreciated. Thanks!
There is a bug in the Slim PHP framework in Environment.php line 143. In particular, it assumes that the $_SERVER['SCRIPT_NAME'] path is compatible with $_SERVER['REQUEST_URI'] variable. In most cases this is probably true, however not when using MOD_REWRITE to hide an intermediate directory (as is happening in the .htaccess you quoted).
What's happening is $_SERVER['SCRIPT_NAME'] looks something like "/public/something..." but (because it is hidden), $_SERVER['REQUEST_URI'] looks like "/something...".
Slim is assuming the request URI is based on the script name, which is not the case here. I plan on notifying the Slim authors of the bug, but wanted to make note of it here as well.
You can fix/work around this by modifying Slim/Environment.php line 143 to this:
if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {
$env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite
$env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
} else {
$env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); //With URL rewrite
$env['PATH_INFO'] = $_SERVER['REQUEST_URI'];
}
// $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
At least that seems to work fine in my case. I believe the intention was to remove the path from the request uri, but that seems a pretty horrible way of doing it. If you need subdirectories to keep working you may need to do a bit more thinking. :)
I have already read a bunch of the articles on stackoverflow about this topic, such as:
CodeIgniter: SEO friendly URLs
Codeigniter routes not working sometimes
And I swear I have set up everything correctly, but after I put the route in and save my app, and attempt to go to the new URL, or even the old one, they both give me a 404 error.
I have an extension that currently looks like this:
search/map_view/county
that I want to look like this:
map/county
I wrote the following reroute in the routes.php file, which gives me the 404 error:
$route['search/map_view/(:any)'] = 'map/$1';
And just in case I was doing it backwards, I also tried it like this:
$route['map/(:any)'] = 'search/map_view/$1';
That didn't do anything, so I've deduced i did that incorrectly. A thing of note is that I do have apache's mod_rewrite changing my url's to drop the index.php from it. Don't know how that's helpful, but I've noticed it a lot in the other posts.
Am I supposed to change something somewhere else for this? I'm assuming that if I type in the previous address, I should get automatically rerouted to the new one? Or if I type in the new address, it should work automatically? I don't know, it's getting really annoying...
Anyhow, I have a lot of questions about this stuff, but I'm going to start here and then see if I can find the rest of the answers here after I fix this one.
EDIT - I've been asked to include more info. Here it is.
Here's the .htaccess content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Currently I don't have any custom routes defined in the routes.php file, just because I can't get it to work correctly.
The current controller is "Search", with the method "map_view" being passed a variable "county". So the url would be
http://www.base_url.com/search/map_view/county
I want to change this to
http://www.base_url.com/map/county
Everything else I've previously written still applies. Thanks again!
You want your url looks like map/country .
In your routes.php
$route['map/(:any)'] = 'search/map_view/$1';
$route['map'] = 'search/map_view';
And be sure your controller name is Search.php.Also class name is Search that extends CI_Controller and method name map_view() (must be public function)
Look CI Controller Guide for detailed information