Search code examples
phpslimslim-3

Got 404 on every request when working on two controllers


I am developing a back end system using slim-3.In app i have multiple controllers like if for Books and Users there are two different controllers in which all calls of each is placed.So when i declare controllers in index.php file then only one controller request is accept other controller request return 404 page not found.When i remove declaration of one controller then other worked. e.g i have two controllers like User Controller and Provider Controller when i declare both of them in index.php then only Provider Controller is worked.But when i remove declaration of Provider Controller then User controllers api calls is working well but when i add the Provider Controller then User Controller api calls return 404. here is index.php code

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

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

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, 
array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");

return $response;
});
require '../src/controllers/UserController.php';
require '../src/controllers/ProviderController.php';
require '../src/models/GeneralResponse.php';
require '../src/database/UserOperations.php';
require '../src/database/ProviderOperations.php';
require '../src/models/User.php';
require '../src/models/Provider.php';
require '../src/utils/Utils.php';

$app->run(); 

here is provider controller

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->post('/provider/register',function($request,$response,$args){
    try{
    $appresponse=new GeneralResponse();
    $formDataArry = $request->getParsedBody();
    $email=$formDataArry['email'];
    $passwordRaw=$formDataArry['password'];
    $firstname=$formDataArry['firstname'];

});

here is User controller

<?php
require '../vendor/autoload.php';
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;
//Registration of user end point
$app->post('/user/register',function($request,$response,$args){
    try{
    $appresponse=new GeneralResponse();
    $formDataArry = $request->getParsedBody();
    $email=$formDataArry['email'];
    $passwordRaw=$formDataArry['password'];
    });


Solution

  • Basically i created separate instance of \Slim\App in every controller these instance overwrite the instance of index file therefore it ignored the require of every controller simply remove $app = new \Slim\App; from controllers file but index.php.Problem will fixed by this. For more information visit this