PHP routing a page using the Slim Framework Ask Question

I’m a PHP beginner so please bear with me… 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, I also have Slim/View. 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?

Update: 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();

?>

If you’re seeing a 500 error in the browser the error should be logged within your webserver. Since you’re using MAMP, find Apache’s error log and have a look.

Something else you might desire is to turn on Slim’s own error reporting. It would look something like this.

$config = [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];
$app = new \Slim\App($config);

However… why are you calling new \Slim\Slim() instead of new \Slim\App[()? You’ve installed Slim 3.x but it looks like you might following some docs from Slim 2.x?

Also, try to get into the habit of leaving out the trailing ?> tag at the end of your files. You don’t need it, and if you accidentally have anything after that closing tag it can blow up your output. :slight_smile:

Thanks for the tips!

I’ve added the code you provided, and it still had the 500, but then I removed a piece of code and a bunch of errors were reported. Not sure if this helps with advising me but here’s the code and the error messages:

 <?php

require 'vendor/autoload.php';

$config = [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];
$app = new \Slim\App($config);

$app->get('/', function() use($app){
  $app->render('about.php');
});
  
$app->run(); 

Removed this:

$view = $app->view();
$view->parserOptions = array(
    'debug' => true

And here are the details:

Type: BadMethodCallException
Message: Method render is not a valid method
File: /Users/chrisioannou/Documents/LocalServ/Chris/vendor/slim/slim/Slim/App.php
Line: 124

Cheers :slight_smile:

You are calling the render method on the App class, but that class doesn’t have a render method.

Is what you pasted your entire app? You may wish to start with a simple skeleton like Slim-Skeleton to get an idea how things work together.

1 Like

That’s the entire thing index.php file. But thank you for the skeleton suggestion, that’s a great idea! I’m going to work on this to get an idea.