Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim Framework - splitting code into multiple files other than index.php

Tags:

php

slim

On the documentation for Slim Framework, it says

In this example application, all the routes are in index.php but in practice this can make for a rather long and unwieldy file! It’s fine to refactor your application to put routes into a different file or files, or just register a set of routes with callbacks that are actually declared elsewhere.

It doesn't say how to actually do this though. My only thought is that you could split code into multiple PHP files and then use include or require in index.php to reference these.

I'm also not sure what it means by "register a set of routes with callbacks that are actually declared elsewhere"

Does anyone have any thoughts on this, since the application I'm wanting to build might have quite a few routes?

like image 703
Andy Avatar asked Oct 06 '16 08:10

Andy


People also ask

What is slim framework in PHP?

a micro framework for PHP. Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. <?php use PsrHttpMessageResponseInterface as Response; use PsrHttpMessageServerRequestInterface as Request; use SlimFactoryAppFactory; require __DIR__ .

What is Slim-Slim?

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. We recommend you install the Slim Framework with the Composer dependency manager. The easiest way to start working with Slim is to create a project using Slim-Skeleton as a base by running this bash command:

How does the slim framework's router work?

The Slim Framework’s router is built on top of the Fast Route component, and it is remarkably fast and stable. While we are using this component to do all our routing, the app’s core has been entirely decoupled from it and interfaces have been put in place to pave the way for using other routing libraries.

How do I start working with Slim framework?

We recommend you install the Slim Framework with the Composer dependency manager. The easiest way to start working with Slim is to create a project using Slim-Skeleton as a base by running this bash command: Replace [my-app-name] with the desired directory name for your new application.


2 Answers

Being a micro-framework, Slim does not enforce any specific method. You can either find a ready-to-use structure (Slim Skeleton Application comes to my mind) or write your own; unlike other frameworks, Slim does not try to protect you from PHP.

Route definitions can be something as simple as an array of strings:

<?php // routes.php
return [
    '/'        => ['Foo\\Home',    'index'],
    '/about'   => ['Foo\\Home',    'about'],
    '/contact' => ['Foo\\Contact', 'form' ],
];

... which you then load and process in your index.php entry point:

$routes = require('/path/to/routes.php');
foreach ($routes as list($path, $handler)) {
    $app->get($route, $handler);
}

And you can leverage the existing Composer set up to auto-load your classes by adding the appropriate directories to composer.json:

{
    "require": {
        "slim/slim": "^3.3",
        "monolog/monolog": "^1.19"
    },
    "autoload": {
        "psr-4": {"Foo\\": "./Foo/"}
    }
}

From here, it can get as complex as required: define routes in a YAML file, auto-load from defined classes, etc.

(Code samples are shown for illustration purposes and might not even be valid.)

like image 189
Álvaro González Avatar answered Oct 09 '22 10:10

Álvaro González


There're some thoughts on it in Slim documentation

Instead of require's you can use composer autoloading


"register a set of routes with callbacks that are actually declared elsewhere"

From docs:

Each routing method described above accepts a callback routine as its final argument. This argument can be any PHP callable...

So you can do:

$routeHandler = function ($request, $response) { echo 'My very cool handler'; };
$app->get('/my-very-cool-path', $routeHandler);

But usually people use classes instead of functions: http://www.slimframework.com/docs/objects/router.html#container-resolution

I think you almost get the basic idea right. I recommend reading chapter on routing a couple of times. It covers everything pretty good.

Happy coding and let me know if you need any other help!

like image 36
Stas Makarov Avatar answered Oct 09 '22 11:10

Stas Makarov