PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] Routing in SlimFramework

 January 21, 2022     composer-php, php, slim     No comments   

Issue

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;

Solution

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



Answered By - Ramy hakam
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing