Run a website with the slim application

Good morning,

English is not my first language, so, please, be indulgent with my faults.
this is my project skeleton:
skelton-slim-secretariat-web

I’m making a website with bootstrap and slim. The webpages are in Views.

  • To launch index.php I run the internal server PHP with:
php -S localhost:8082 -t public
  • I have named the entry of the website “accueil.php” in Views. I have made a route ('/accueil) to require accueil.php. I don’t if it’s the best way, To run it, i open firefox with this URL: localhost:8082/accueil. This page works. But not the internet menu Bootstrap. So it’s not a good idea.

The public folder is become the reference. I tried to see:

php -S localhost:8082 -t src/Views

Of course, the site works without css

How can I resolve this problem?
Do I have to use apache?

Thanks and have a good day.

The public/ directory should be the only access point to your Slim website/application from the web. So you should never use internal directories like ‘src/Views’ as the document root for your web server.

But not the internet menu Bootstrap. So it’s not a good idea.

Can you please explain this with more details?

In “routeur.php”, i put:

return function (App $app) {
    $app->get('/accueil', ControlleurAccueil::class . ':accueil'); //lancement de la page accueil.php
{

and in the function accueil,

public function accueil(Request $request, Response $response) : Response{
        // Inclure le fichier accueil.php
        include (__dir__ . '/../Views/accueil.php');
    }

I’m thing it’ too complicated:

  • I can bring together these two parts
$app->get('/accueil', function (Request $request, Response $response, $args) {
    // Inclure le fichier accueil.php
    include (__dir__ . '/../Views/accueil.php');
});
  • it’s not very good to use include, in this case. I perhaps use “render” instead, but I haven’t the syntax to use it with the Php files. Is php-view permit to create render?
  • With Slim it’s preferable to make one route per page?
  • can Php internal server resolve the problem of the views and index.php? Or do I have to use Apache?

For the Bootstrap menu, I think it needs routes with rendering functionality like buttons instead of URLs. That’s my assumption. Is it wrong?

Instead of the include function, you can use the slim/php-view package, which allows you to render the PHP template directly into the Response object.

Simple example:

Run: composer require slim/php-view

use Slim\Views\PhpRenderer;
// ...

$app->get('/accueil', function (Request $request, Response $response) {
    $renderer = new PhpRenderer(__DIR__ . '/../Views');

    return $renderer->render($response, 'accueil.php');
});

With Slim it’s preferable to make one route per page?

Yes

Can PHP internal server resolve the problem of the views and index.php? Or do I have to use Apache?

The PHP internal server is just for development purposes. In production, you would use Apache for example. Generally, it works with any type of Webserver that runs PHP. Take a look at my example (see above).

For the Bootstrap menu, I think it needs routes with rendering functionality like buttons instead of URLs. That’s my assumption. Is it wrong?

It depends. Usually the server returns a html page, that loads other resources such as JS and CSS files for Bootstrap and other website features.

Thanks, Odan.
The website works with LAMP.
I follow this page for the configuration: Web Servers - Slim Framework
I create one route per page.
For the bootstrap navbar, i use links like href=“accueil” (without/ . I found it with a mistake!)
And for the button, I have to use the complete URL without .php like http://localhost/slim-secretariat-web/inscription
Good night.

href=/slim-secretariat-web/inscription works also for the button.

href=/slim-secretariat-web/inscription works also for the button.

Ok. Just make sure that this path is the same in your production environment.