Search code examples
phpslim

How to call internal route?


Suppose I declared an API route for /test inside a file called foo.php, how can I access this route /test from another file?

Example (foo.php):

$app->get('/test', function (Request $request, Response $response, array $args)
{

});

I want access to this route from test.php, how can I do that?


Solution

  • Instead of having one very large foo.php file, there are several smaller files that make building a larger application much easier.

    test.php

    $app = new Slim();
    
    require 'foo.php';
    
    $app->run();
    

    foo.php

    $app->get('/test', function (Request $request, Response $response, array $args)
    {
        return 'Hello World!';
    });
    

    How to organize a large Slim Framework application