Skip to content

Instantly share code, notes, and snippets.

@odan
Last active January 15, 2022 11:22
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odan/c8bee474b0054a06776481a6c8de1d8f to your computer and use it in GitHub Desktop.
Save odan/c8bee474b0054a06776481a6c8de1d8f to your computer and use it in GitHub Desktop.
Slim 4 Tutorial
@juanma-mol
Copy link

Ok, I solve the problem:
in src/container.php add:

use Psr\Http\Message\ResponseFactoryInterface;

//..

    ResponseFactoryInterface::class => function (ContainerInterface $container) {
        return $container->get(App::class)->getResponseFactory();
    },

@blacktornado
Copy link

blacktornado commented Oct 5, 2020

Hi @odan ! fantastic tutorial . I am following it right now, but i customized it as per my requirement and didn't worked so far. If i start my local server with php -S 0.0.0.0:8080 -t public public/index.php it works like a charm. but on real server i cannot start the server explicitly for this project as there are other php project running. so i had to add $app->setBasePath('/somefolder/slim4')

here in container.php if i add these lines it wont work like this

`App::class => function (ContainerInterface $container) {  
     AppFactory::setContainer($container);
     $app = AppFactory::create();
     $app->setBasePath('/somefolder/slim4'); //this doesn't work from here rather had to write again in router.php
     //var_dump($app);
    #return AppFactory::create();
    return $app;
},`

i am again adding / creating the instance here in routes.php // also notice i have removed return function(App $app){ //this is not working

$app = AppFactory::create();
$app->setBasePath('/somefolder/slim4'); //now it works

$app->get('/', \App\Action\HomeAction::class)->setName('home');

and in bootstrap.php i changed it to

// Register routes (require __DIR__ . '/routes.php');//($app); //notice i removed $app from passing as we have removed return function(App $app){ from router.php

but i can see its a bad way to fix this issue, any other way or am i missing something. how to fix this
Thanks in advance

@odan
Copy link
Author

odan commented Oct 5, 2020

Hi @blacktornado

The Slim 4 tutorial already contains the solution for exact this issue.
Just install and add the BasePathMiddleware as described here:

https://odan.github.io/2019/11/05/slim4-tutorial.html#base-path

@blacktornado
Copy link

blacktornado commented Oct 6, 2020

Hi @odan, thank you for the response.
just downloaded your code from github, yes BasePathMiddleware added. As said everything works fine when i start the php server inside public/index of my slim4 project directory like `php -S 0.0.0.0:8080 -t public public/index.php

but i get 404 when i start the server outside the slim4 folder like php -S 0.0.0.0:8080
i am wondering how the program is determining which folder to run in browser because there are so many other php project.

error image

@odan
Copy link
Author

odan commented Oct 6, 2020

@blacktornado You screenshot shows that you start the php internal webserver directly in your Desktop directory. I'm sure this is not the correct path where you have stored the public/ directory. Change into the project directory and then into the public/ directory.
I think in your specific case it should be:

cd c:\Users\black\Desktop\slim
php -S localhost:8000 -t public

You can also try this:

cd c:\Users\black\Desktop\slim\public
php -S localhost:8000

Then navigate to: http://localhost:8000/

For technical questions create an issue here: https://github.com/odan/slim4-tutorial/issues

Read more: odan/slim4-skeleton#7

@blacktornado
Copy link

blacktornado commented Oct 6, 2020

yes @odan i understand your point as it is looking for the public directory which has index.php, as i am using php internal web server.

coming to another scenario where i have web server that points to htdocs and my slim is inside htdocs/somefolder/slim i am hitting the url with 10.xxx.xxx.x:port/somefolder/slim the request lands successfully to public/index.php from there it goes to bootstrap.php to container.php and finally lands to routes.php where if i do a var_dump($app) it gives me resultset but i am getting 404 error with

Type: Slim\Exception\HttpNotFoundException
code: 404
Message: Not Found
File: /www/zendphp7/htdocs/somefolder/slim/vendor/slim/slim/Middleware/RoutingMiddleware.php

to make it work if i remove this line return function(App $app){ from router.php and again add this two lines in router.php
$app = AppFactory::create();
$app->setBasePath('/somefolder/slim');
its working.

@odan
Copy link
Author

odan commented Oct 6, 2020

@blacktornado I think it makes no sense to create a new $app instance, because per request there should be only one Slim instance.
If you have further technical questions, please create an issue here: https://github.com/odan/slim4-tutorial/issues

@marcusball
Copy link

marcusball commented May 29, 2021

This is a great tutorial, thank you so much!

Is there any particular reason to separate the Read and Create into separate repositories and services? In this example, would it be against design patterns to use a single UserRepository containing both insertUser and getUserById methods? Similarly, is it suggested to not combine the Service methods into a single UserService?

@odan
Copy link
Author

odan commented May 29, 2021

Hi @marcusball Please note that this is not an Active-Record (anti-) pattern. The shown pattern follows Command–query separation (CQS) on routing level. So read and write access is separated. To respect the SOLID (see SRP) principles, the service class is responsible only for one specific task (use case) and not more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment