2

I follow a tutorial for SlimFramwork and I try to route through some pages that I defined. I have this index.php file from which I run:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

require_once('../app/api/books.php');

$app->run();

?>

and this file books.php which is my page:

<?php

$app->get('/api/books', function()  {
    echo "Welcome to books";
});

?>

This is my folder structure:

Folder structure

I run this on localhost with wamp using this link:

localhost:8082/myslimsite/app/api/books

I got my localhost on port 8082, my php version is 7.0.10

This are my errors

Errors

I try to find on the internet solution but nothing works, something that I found and tried was:

1 - For books to use ($app) like this:

<?php

$app->get('/api/books', function()  {
    echo "Welcome to books";
});

?>

2 - In books to use the file with the class that have $app variable

require '../vendor/autoload.php';
$app = new \Slim\App;
hassan
  • 7,812
  • 2
  • 25
  • 36
Wolf Marian
  • 147
  • 12
  • What tutorial are you following? – OptimusCrime May 14 '17 at 14:31
  • the question is : why do you need to separate your routes like this? put all of your routes in one file. – hassan May 14 '17 at 14:36
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – hassan May 14 '17 at 14:41
  • It is unlikely that the code you posted creates the error message you get. Please check again that your posted code is exactly what you see in your files. This is likely a typo inside one of the files. – Sven May 14 '17 at 17:10
  • The posted code is the exact one that I use. This is the tutorial that I follow: https://www.youtube.com/watch?v=C0V9vj2lYUY&index=3&list=PLBEpR3pmwCayt4DR0UbhMgCfxHQWi0RCQ – Wolf Marian May 15 '17 at 07:04

1 Answers1

0

Your route is not correct You must navigate first to index.php then to your route

IF your server home page is index.php in the public folder You will only navigate to your route url directory NOT to the path for This file Otherwise you must navigate to public/Your-Route-Url

So Navigate to

localhost:8082/myslimsite/public/api/books

This will Work correctly

Ramy hakam
  • 522
  • 1
  • 6
  • 19