Trouble integrating Slim php and client side application - php

I have a Slim application with following structure
project_root/
index.php
Slim
Client
index.html
scripts
styles
partials
I need to do the following:
Slim/PHP application should act as a rest server that responds in json for urls starting with /apis
Other urls should be redirected to Client and resources viz. styles,scripts should be served statically.
Client application is an independent application. (in this case an angular application)
index.php contents are like
$app = new \Slim\Slim();
$app->add(new \Slim\Middleware\ContentTypes());
// Routes
$app->get('/apis/register',function(){
echo "apis/register";
});
The following route renders index.html page. initial page of my angular application.
But styles and scripts are not getting served.
I get 404 Not Found for styles and scripts.
$app->get('/:route',function($route) use ($app) {
$app->config('templates.path', './Client/');
$app->render('index.html');
})->conditions(array("route" => "(index|style)"));
How can i host styles and scripts that are used by client application?
May be through some Slim route(preferred) or through apache configuration
In rails we can write following routes.
match "/styles" => redirect("/")
match "/partials" => redirect("/")
EDIT:
Looks like /* is not yet supported in Slim or its missed in documentation.
Like in node.js we do,
{
path: '/*',
httpMethod: 'GET',
middleware: [function(req, res) {
res.render('index');
}]
}

It seems that Slim has some kind of bug for redirecting to non-existing sub directory .htacess rewrites. I had a similar problem. I actually created the sub-directory and made the .htaccess to direct all API calls to specific sub directory and avoid App directory.
Have a look at this. It eventually solved my problem.
UPDATE: have a look at this at the Slim Help Thread. They had this issue indeed.

You can use these instructions (Apache Module mod_rewrite) in your .htaccess file to only send non-existing files to Slim.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Related

How to access slim app requests on a shorten url

I have a simple SLIM application that responds to all requests to sent it in an API-like way for example the following url:
http://example.com/pages/subdirectory/slimApp.php/products
As you can see, the url to the slim app is absolute and pretty long but I have tried using Apache2 mod_rewrite to shorten the url as shown below
RewriteEngine on
RewriteBase /pages/
RewriteRule ^slimApp/(.*) subdirectory/slimApp.php/$1 [L,NC]
The code above rewrites to the main route of the app which is basically a 404 error page and acts like no request has been specified. When I access the app with an absolute url, it works as normal but if I try to access /products from the shorten url version, it fails with a slim 404 page.
Basically what I need is to translate the following and still be able to handle requests like shortened_url/products, shortened_url/something_else, etc. For-example:
http://example.com/pages/subdirectory/slimApp.php/products
to
http://example.com/slimApp/products
Bear in mind that /products is dynamic, any help is greatly appreciated.

Conflict between SVN and Web Application - routing

I have installed SVN and use it to maintain access to my repositories via the http protocol (http://localhost/svn/project_name/ maps to the physical location /var/www/repos).
I also have Apache installed for my web application (with a physical location of /var/www/web_app). This can be access via http://localhost/, rendering a default action (Presenter: Homepage, Action: default - this yields index.php).
However, when I want to view the Contact Page (Presenter: Contact, Action: default - yielding http://localshots/contact) it says:
NOT FOUND The requested URL /contact was not found on this server.
The error doesn't look like it is a framework error (bad code for routing in the framework or something similar). It looks like there is a conflict between in .htaccess or the apache config.
I'm using a PHP framework (called Nette, but I think it's not important what framework is being used: I think the problem lies between the routing and apache configs).
So, it seems that you have no rewrite rule for the url you are requesting.
2 possibility here, you write a new rule like :
RewriteRule ^[/]*contact$ index.php?page=contact [L]
The second is to modify the url you want to reech like http://localhost/contact.php, then create a php file a the root of your project.
EDIT :
You can also do it like this :
RewriteRule ^[/]*contact$ contact.php [L]
So you have a beautiful url and redirect directly to your file

PHP framework work flow

I am confused about how a php framework works with a web server like apache. Now if there wasn't a framework and if it was using classic php, then I understand how it works(if a php file is requested, the php parser executes it and returns the htm to the server).
When a framework like cakePHP is used, I have noticed that no matter which url the client requests, the index.php in the root folder gets executed first. How is this possible? if we were using pure php then, only the file we requested will get executed. So how does cakePHP make each and every request to go through the /index.php file?
CakePHP, and many other websites, leverage mod_rewrite which is an Apache module that "Provides a rule-based rewriting engine to rewrite requested URLs on the fly".
To do so, the web framework will use a .htaccess file with specific rules defined. These rules, in the case you're asking about, point any URL matching a specific pattern to to a real index.php file (this file can be named anything really, but index.php is a good "default" name).
Per CakePHP's URL Rewriting outline, a base rule is defined as:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/to/cake/app
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
What the above does is:
Turns on the rewrite module
Sets two conditions for rewriting the current URL
a. Only if the requested file is not a real directory (!-d)
b. Only if the requested file is not a real file (!-f)
Sends the full requested URL, (.*) to index.php
Sometimes, you'll see the RewriteRule line as:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This will set the requested url into the $_GET['url'] parameter instead of forcing the application to process the $_SERVER['REQUEST_URI']. Other times, there will be a lot of rewrite rules, some complicated, some basic.
There are a lot of good resources online, and in books, regarding mod_rewrite. Check out Introduction to URL Rewriting for one (it's one I have bookmarked, has a lot of good basic rules / formats).
That's a pretty simple one (https://github.com/gilbitron/PIP).
Read the files in the System folder.
Basically it goes like this:
All requests are sent to the index file (with mod_rewrite, or like a plain get parameter).
The index file includes configuration files and definitions of constants.
Then it loads the main class for the framerwork.
The main class does some configurations, initiates a database connection and so on.
Depending on how it is implemented, there might be a router but it's possible there isn't.If there is one the main class checks what controller should be loaded against the request in the router.
Then the main class loads the controller that should be loaded with that particular request.
PIP is not that good and the main class is actually not a class but you can get the point in it.
You can also read about the following things:
Object oriented programming: http://php.net/manual/en/language.oop5.php
Scopes: http://php.net/manual/en/language.variables.scope.php
Autoloading classes: http://php.net/manual/en/language.oop5.autoload.php
You mentioned
"I have noticed that no matter which url the client requests, the index.php in the root folder gets executed first."
This is what front controllers do. You can have a front controller with a framework or if coding from scratch using the .htaccess file. Front controllers enable rendering of pages using PHP functions/methods, such as $app->get($uri, $callback); in Lumen.

laravel 4 routes with ?_escaped_fragment_=

I am trying to enable clean urls with Angular JS inside my Laravel 4 app. When I tried the required thing normally without laravel 4 refering to this url.
http://www.yearofmoo.com/2012/11/angularjs-and-seo.html, I was able to get the thing to work. Even ?_escaped_fragment_= was changed to snapshot/* folder and picked the content.
But I am not able to do the same in Laravel 4. I am not sure how to do this.
I am trying to define a route like ?_escaped_fragment_=/* and redirect it to some controller but that doesn't work.
Can anyone please help.
Escaped fragment is a query parameter, not a route, there for, in the root route Route::get('/', 'RootCtrl');, you can check for $_GET['_escaped_fragment'] presence, and if it is there, return an HTTP 302 redirect to the corresponding file in the snapshots dir.
While this will work, it is not the perfect solution, since some bots might index the path of the snapshots file instead of the original one, and since doing it using the Laravel framework is not correct in the first place.
The best choice of course, is to rewrite any request that contains an _escaped_fragment_
query parameter to it's corresponding path, in the nginx\apache configuration, prior to the configuration of your Laravel app, this way those requests won't event reach the Laravel router.
I have co-authored an Angular SEO plug-and-play solution, using PhantomJS & Mongoose integrated web server, to pre-render any Async JS code, and server it as raw HTML.
The server configuration aspects are also explained in the README.md file, please note that if you would like to use static snapshots, just change proxy-pass to rewrite(in order for the right URL to get indexed by the bots).
Hope that helps.
Example using simple rewrites with nginx:
if ($args ~* "^_escaped_fragment_=(.+)") {
set $path $1
set $args '';
rewrite ^.*$ /snapshots/$path last;
}
EDIT:
Apache, for your request (some words of advice: move to nginx).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment=(.+)$
RewriteRule ^.*$ /snapshots/%1 [QSA,L]
</IfModule>

How can I clean up Symfony admin routes?

My backend URLs look like this:
mysite.com/backend.php/blog
I'd like to change it to:
mysite.com/backend/blog
Technically this isn't limited to admin apps, as Symfony grants every application two front controller scripts. But I hate having the script name in URLs and as such I'd like to change it. Is this possible?
Thanks in advance.
Edit
#codecowboy - I did resolve this by creating a 'backend' directory in the web directory. I then copied over the .htaccess file symfony puts in web, and I moved the backend.php and backend_dev.php front controllers to /backend and renamed them index.php and index_dev.php. Then within each front controller I tell PHP to look one directory further up for the project config class. I've been doing this for a while now and it serves my needs perfectly. I actually wrapped this all up in a task so that setting up a new admin app is a 1 step processs.
You can add
#I'm no regular expression expert or mod_rewrite expert, this line probably has some bugs
RewriteRule ^backend(.*)$ backend.php [QSA,L]
to your .htaccess file right before
RewriteRule ^(.*)$ index.php [QSA,L]
and that solves 1/2 of your problem. Anything sent to yoursite.com/backend/xxx will be routed through backend.php. The other problem you get is with internal symfony routing. It will interpret yoursite.com/backend/xxx as a request for module "backend" and action "xxx". I'm sure it's not too hard to solve. Good Luck!

Categories