How to use Slim4

Hello, beautiful PHP people. I need some help which I could not find in the documentation. I am new to Slim and I am trying to run a project with Slim 4. However, I am having difficulty using Slim 4. I followed the example in the URL below which seems to work only for older versions.

https://pastebin.com/3PYqECrJ

The brackets at the very end here $app = new App(); are highlighted by the IDE and when I hover it says: Required parameter $responseFactory missing.

And when I run the project it shows this error:

Fatal error : Uncaught ArgumentCountError: Too few arguments to function Slim\App::__construct(), 0 passed in /opt/lampp/htdocs/vruummates/app/start.php on line 14 and at least 1 expected in /opt/lampp/htdocs/vruummates/vendor/slim/slim/Slim/App.php:57 Stack trace: #0 /opt/lampp/htdocs/vruummates/app/start.php(14): Slim\App->__construct() #1 /opt/lampp/htdocs/vruummates/index.php(3): require(’/opt/lampp/htdo…’) #2 {main} thrown in /opt/lampp/htdocs/vruummates/vendor/slim/slim/Slim/App.php on line 57

Does anyone have any idea why is that and what am I not doing right? Thank you in advance for your help.

Hi :slight_smile:

In slim 4, if you do not want to inject the needed dependencies into App(), you should use the Factory provided.

use Slim\Factory\AppFactory;
$app = AppFactory::create();

Slim website has it on the front page http://www.slimframework.com/

Hope it helps.

1 Like

Wow,

I did not expect someone to respond so quickly. Thank you!

I saw that and tried it too, but I get another error which I was googling to resolve but could not find anything. I appreciate your help. Here is the error:

Fatal error : Uncaught RuntimeException: Could not detect any PSR-17 ResponseFactory implementations. Please install a supported implementation in order to use AppFactory::create(). See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations. in /opt/lampp/htdocs/vruummates/vendor/slim/slim/Slim/Factory/AppFactory.php:166 Stack trace: #0 /opt/lampp/htdocs/vruummates/vendor/slim/slim/Slim/Factory/AppFactory.php(92): Slim\Factory\AppFactory::determineResponseFactory() #1 /opt/lampp/htdocs/vruummates/app/start.php(14): Slim\Factory\AppFactory::create() #2 /opt/lampp/htdocs/vruummates/index.php(3): require(’/opt/lampp/htdo…’) #3 {main} thrown in /opt/lampp/htdocs/vruummates/vendor/slim/slim/Slim/Factory/AppFactory.php on line 166

You are Welcome :slight_smile:

I think there is always someone around.

You need to provide a PSR 7 library.
Slim has one: slim/psr7, but you can use others.
The App Factory can autoload Guzzle, Nyholm, Zend and Slim ones. Just use composer to require one of those.

For example:
composer require slim/psr7

More info on:
http://www.slimframework.com/docs/v4/start/installation.html

1 Like

Great! :slight_smile: Thanks very much for your help, indeed. I am getting there.

I did that, but now it is showing the error below. I found a few things in stackoverflow and I am going to try them tomorrow. Once again, thanks a lot. I can’t wait to start working on this project :slight_smile:

Warning : require(/opt/lampp/htdocs/vruummates/vendor/composer/…/illuminate/support/helpers.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/vruummates/vendor/composer/autoload_real.php on line 66

Fatal error : require(): Failed opening required ‘/opt/lampp/htdocs/vruummates/vendor/composer/…/illuminate/support/helpers.php’ (include_path=’.:/opt/lampp/lib/php’) in /opt/lampp/htdocs/vruummates/vendor/composer/autoload_real.php on line 66

For what I can see there is something that is not in the right place but is clearly not related to Slim.

I would start by deleting all composer dependencies and run composer again to do a clean install.

Don’t know if it will help but it seems like a simple thing to try. Like restarting a computer when something is not working properly.

1 Like

Thanks a lot for your help. I fixed it. It works now. I uninstalled all dependencies and then installed them one by one as you suggested. At the end I installed slim/psr7. I also cleared cash on Composer and updated it. And it works as expected! YAY!

Glad I could help you :slight_smile:

1 Like