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.
Related
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.
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.
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();
Would like to integrate Laravel4's into an existing flat PHP-MySQL site,
while I am applying MCV logic page-by-page I need to keep this site work normally.
Before moving ahead: Q1.Does this intergration work at all ? Q2.Does anyone foresee any problem ?
Sofar I have only done this:
app/routes.php
Route::get('/', function(){
//just keep empty, index.php shows up as intended.
});
public/index.php
test
<?php
//Codes transplanted from Laravel4
//.......start......
/**
* Laravel - A PHP Framework For Web Artisans
*
require __DIR__.'/../bootstrap/autoload.php';
It won't quite work this way. In the middle of MVC we have our "Views" which are what laravel will return to the user for display.
What you can do, though, is put your index.php folder in the app/views directory. If, for example, you put the content of your index.php file in:
app/views/index.blade.php
You can then call it via:
Route::get('/', function(){
return View::make('index');
});
This will have the added benefit of getting you through your first step of moving from a "flat" PHP site and in to the framework.
Note: If you DO try using the implementation you provide in your example, you're going to get a bunch of "Not Found" exceptions.
I'm using Slim. In the documentation they only show examples working with only one index.php file, which has really little functionality for every route. For example:
$app = new \Slim\Slim();
$app->get('/books/:id', function ($id) {
//Show book identified by $id
});
But in my case, my index.php file is getting bigger and bigger, now I have a lot of code for most routes, what is the best practice in this case? to include files inside the routes closures? What happens with the scope of global variables, like DB connection or app config? Thank you
Brian Nesbitt made a nice post about this: http://nesbot.com/2012/11/5/lazy-loading-slim-controllers-using-pimple.
If you don't want to use pimple, than you can get some idea from the section "Common first attempt", on how to separate you files.
update:
Since version 2.4.0 you can use the inbuilt "Class controller": Version 2.4.0