File extensions in Slim routes - php

Why does Slim 404 when I try to add an extension to the route?
<?php
require_once 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/test', function () {
echo 'route 1';
});
$app->get('/test.html', function () {
echo 'route 2';
});
$app->run();
http://localhost:8080/test works
http://localhost:8080/test.html throws a 404

I assume you are running the example using internal PHP webserver. Built-in server will consider request as a static file request if there is a dot in the SCRIPT_NAME. It is considered a feature and not a bug. See PHP bug #61286 for more info.
Your code should work fine with Apache or other webservers, assuming rewrite rules are set up correctly.

Related

Slim 4 internal redirect, infinity loop

After upgrading to Slim v4 I'm trying to replace my simple $app->subRequest call with $app->handle as specified in the changelog. However there are no details on how to do this in either the changelog or upgrade guide and my best effort to fix it ends up creating an infinite loop:
$app->get("/foo", function (Request $req) use ($app) {
$uri = $req->getUri();
$newUri = $uri->withPath("/bar");
$barReq = $req->withUri($newUri);
// Here we get stuck in endless loop instead of ending up in the /bar route handler below
$app->handle($barReq);
});
$app->get("/bar", function (Request $req) use ($app) {
echo 'bar!';
die;
});
It's like even though $barReq is a new request object with a completely new uri (and path) the router does not resolve which route handler that should handle it, instead it's just handled by the same one again.
My previous simplified (v3) code looked like and worked fine to get the result of the /bar route when calling /foo:
$app->get("/foo", function (Request $req) use ($app) {
$app->subRequest('GET', '/bar');
});
I'm probably missing some central concept on how Slim 4 handles requests and routes internally and would appreciate some help!
Edit: Should perhaps add that what I mean with internal redirect is that client should not be aware that a redirect has been made. I.e. any regular redirect function returning something to client is not applicable here.
As #remy stated, use the ServerRequestFactory implementing ServerRequestFactoryInterface.
For slim/psr7 it is: Slim\Psr7\Factory\ServerRequestFactory
A silent redirect to another route is then as simple as:
use Slim\Psr7\Factory\ServerRequestFactory;
...
...
$app->get('/foo', function ($request, $response, $args)
{
global $app;
return $app
->handle((new ServerRequestFactory())->createServerRequest('GET', '/bar'));
});

Slim Php not recognizing gets

I am just starting to work with Slim PHP. What could be the reason that get is not recognized on server?
This route works: it returns required text https://mywebsite/back/public
This route doesn't work (Not Found): https://mywebsite/back/public/countries
I have just installed slim framework and added new index.php file.
<?php
require '../vendor/autoload.php';
$app = new \Slim\App();
$app->get('/', function($request, $response, $arg) {
$response->write("This route works");
});
$app->get('/countries', function($request, $response, $arg) {
$response->write("This route doesnt");
});
$app->run();
?>
What version are you using? If you are using version 4, you need to set the basePath.
$app->setBasePath('/back/public');
Also enable mod_rewrite and configure your .htaccess file.
I recommend you check this project on github slimphp/Slim-Skeleton. This is a boilerplate code for Slim 4 applications.

Serving Static Files with Slim Application v3 Using Built In PHP Server

I'm starting a new project using Slim framework. So far it's going decently, everything I'm looking for in a micro framework. I have been away from the PHP world for a while so I am now getting to know the built in PHP web server. The only issue is this: How do I serve my static content?
For reference, here is my project layout:
project:
public folder
index.php
static/css/style.css
templates/index.html . # I'm using twig (coming from python Flask)
My template:
<html>
<link href="{{ base_url() }}/css/agency.css" rel="stylesheet">
<!-- other cool layout stuff -->
and my index.php (very minimal)
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->get('/', function ($request, $response, $args) {
$response = $this->view->render($response, 'base.html');
return $response->write("Hello ");
});
$app->run();
When I start the built in php server from the command line, I do this:
php -S localhost:8080 -t public public/index.php
While this works great, when I try to access my static content, it just returns the rendered base.html file
Please let me know the best way to start this so static content is rendered correctly. Your help is appreciated.
So based on the documentation as pointed out by kuh-chan and Nima, I expanded it for both Slim purposes, as well as returning a 404 response if the file did not exist.
if (PHP_SAPI == 'cli-server') {
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
// check the file types, only serve standard files
if (preg_match('/\.(?:png|js|jpg|jpeg|gif|css)$/', $file)) {
// does the file exist? If so, return it
if (is_file($file))
return false;
// file does not exist. return a 404
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
printf('"%s" does not exist', $_SERVER['REQUEST_URI']);
return false;
}
}

PHP routing a page using the Slim Framework

I haven't found many questions and answers relating to this so far, so thought I would ask the question as it will greatly help me out as a beginner learning PHP and the Slim framework. It's pretty straightforward (i think).
So, I want to route my home page to another page called about.php. I'm using the Slim/Slim framework which is installed in my vendor folder. And I have an index.php file with the following code:
<?php
require '/vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/', function() use($app){
$app->render('about.php');
});
$app->run();
?>
I also have an about.php file, which does exist.
This is what's currently in my composer.json file:
"require": {
"monolog/monolog": "^1.22",
"slim/slim": "^3.7",
"twig/twig": "^1.32",
"slim/views": "^0.1.3"
}
When I run MAMP (set-up to access the project I am working on) to see the page, it's blank. Can anyone help me understand what I'm doing wrong?
I've then run this (removing the leading '/' from the require statement, and adding a line to display errors), and it displayed a 500 error:
<?php
ini_set('display_errors', 1);
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$view = $app->view();
$view->parserOptions = array(
'debug' => true
$app->get('/', function() use($app){
$app->render('about.php');
});
$app->run();
?>
I'm unclear if you want about.php to be separate from your Slim application or not.
i.e. when someone goes to http://example.com/ do you want the browser's URL to change to http://example.com/about.php where about.php is a completely independent PHP file in the same directory as your index.php?
If you do then you need to redirect:
$app->get('/', function ($request, $response) {
return $response->withRedirect('/about.php');
});
More usually, Slim is used to route to and display all pages in your application and in this situation, you wouldn't see the .php in the URL. This is because our app always runs index.php regardless of the actual URL in the the browser's address bar.
In this situation, you would probably still redirect, but without the .php:
$app->get('/', function ($request, $response) {
return $response->withRedirect('/about');
});
You would also need a handler for /about otherwise Slim won't know what to do:
$app->get('/about', function ($request, $response) {
return $response->write("This is my about page");
});
We don't actually tend to write the HTML directly in our handler though. We use a renderer. There's two in the Slim project: PhpView and TwigView. The Skeleton application shows how the PhpView version works. You may also find the tutorial in the documentation useful.
Finally, if you see a 500, then you have a PHP error somewhere. The easiest way to find this is to ensure that the php.ini setting error_reporting is set to E_ALL and that display_errors is set to On.
As you've already discovered, Slim 3's main class is called App. You can also enable detailed error displays within Slim using:
$app = new Slim\App(['settings' => ['displayErrorDetails' => true]]);
Again, usually we have a separate settings.php file containing this configuration information as shown in the skeleton application.

Slim PHP custom http response pages

Ok I have read the docs on Slim PHP and read quite a few tutorials, and have an application nicely under way. However I am trying to get to grips with things such as customizing the HTTP response codes. I have managed to get the following :
$app->notfound('template.file', array(
'data' => 'passed'
));
This seems to work nicely (as it should as it is a Method directly within Slim), however not I am trying to control things such as the 403 response. I have controlled the not permitted using Apache as I usually would do, however I am wondering if there is a way with Slim that I can serve a custom Not Permitted page? Or because I have blocked it at the Apache level, will Slim not even notice?
I did read that I can manually halt certain routes? Is this the way I should do it? For example, I don't want people to access my JS directory so :
$app->group('/js', function () use($app) {
$app->get('/', functin () use($app) {
$app->halt(403, "You shall not pass!");
});
});
My reason for grouping this would be because I want access to my actual scripts, just not directory browsing.
Has anybody come across this before? What would you suggest? Or am I totally over thinking something simple....
You could simply redirect user to another route where you print your custom template. Example:
require 'vendor/autoload.php';
session_start();
$app = new \Slim\Slim();
$app->group('/js', function () use($app) {
$app->get('/', function () use($app) {
$app->flash('httpStatusCode', '403');
$app->redirectTo('NotAuthorized');
});
});
$app->get('/notAuthorized', function () use($app) {
echo 'You\'re here because you\'re Balrog!';
echo 'Http Status Code: ' . $_SESSION['slim.flash']['httpStatusCode'];
})->name('NotAuthorized');
$app->run();

Categories