Fatal error: Uncaught Slim\Exception\HttpNotFoundException in Slim 4.7 - php

Im trying to launch my app , but everytime i open in my browser http://localhost/ it gives me this error :
/* namespace handles; use connection\Sql; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; $app = AppFactory::create(); $sql = new Sql(); $stmt = $sql -> comand( "SELECT * FROM substancias" ); $app->get('/hello/{name}', function (Request $request, Response $response, $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run(); var_dump($stmt); */
Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:91 Stack trace: #0 C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request))
#2 C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#3 C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request))
#4 C:\Users\mathe\Desktop\conscious\backend\vendor\src\index.php(24): Slim\App->run()
#5 {main} thrown in C:\Users\mathe\Desktop\conscious\backend\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 91
I already tried to setBasePath ,but still not works , can someone helps ? thanks
Index.php code inside C:\Users\mathe\Desktop\conscious\backend\vendor\src :
<?php
namespace src;
header("Access-Control-Allow-Headers: *");
require("../autoload.php");
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use handles\userhandle;
$ei = "test";
$app = AppFactory::create();
$app->setBasePath('/myapp');
$user = new userhandle();
$app->get('/aki' , function(Request $request, Response $response){
global $user;
$user->VER($request, $response);
});
$app->run();
var_dump($ei);
?>
userhandle.php inside C:\Users\mathe\Desktop\conscious\backend\vendor\src\handles :
<?php
namespace handles;
header("Access-Control-Allow-Headers: *");
require("../../../vendor/autoload.php");
use connection\Sql;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class userhandle {
private $sql;
public function VER(Request $request, Response $response){
$this->sql = new sql();
$stmt = $this->sql -> comand(
"SELECT * FROM substancias"
);
$response->getBody()->write($stmt);
return $response;
}
}
?>

after -> $app = AppFactory::create();
add -> $app->addErrorMiddleware(true, true, true);
add -> $app->setBasePath("/project_folder/api"); //folder where index.php is located
this is my .htaccess (located also api folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Related

Understanding Slim 4 routing function

I am trying to learn slim framework and I am following the tutorial. What I would like is a detailed explanation of what the be low snippet of code is doing within the slim environment.
$app->get('/client/{name}'
The reason that I am asking is because in keep getting route not found. But I have yet to figure out why. The base route works. But when I added the twig and tried to route to that. It fails.
Now comes the code:
This part is in my webroot/public/index.php
<?php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Twig\Error\LoaderError;
require __DIR__ . '/../vendor/autoload.php';
$container = new Container;
$settings = require __DIR__ . '/../app/settings.php';
$settings($container);
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
// Create Twig
$twigPath = __DIR__ . "/../templates";
$twig = '';
try {
$twig = Twig::create($twigPath, ['cache' => false]);
} catch (LoaderError $e) {
echo "Error " . $e->getMessage();
}
// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
$app->run();
This part is in the routes.php:
<?php
use Slim\App;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
return function (App $app) {
$app->get('/', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Hello world! Really?");
return $response;
});
$app->get('/client/{name}', function (Request $request, Response $response, $args) {
$view = Twig::fromRequest($request);
return $view->render($response, 'client_profiles.html', [
'name' => $args['name']
]);
})->setName('profile');
};
The first route works fine the second does not. According to what I am reading. It should work. https://www.slimframework.com/docs/v4/features/templates.html
I feel that if I knew what get is looking to do. I may be able to fix it and build a proper route.
When I dig into the $app->get which connects with the RouterCollecorProxy.php. There is the $pattern variable and $callable. The callable is the anonymous function that comes after the common in the
$app->get('/client/{name}', function <- this is the callable, right?
I see the map class which takes me to the createRoute which returns the $methods, $pattern, callable and a few other things.
I think the pattern is where my problem is.

Slim 4 - Registering and Accessing Routes

I have just created slim 4 project using "composer create-project slim\slim-skeleton appdb". I have a routes.php as
<?php
declare(strict_types=1);
use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
return function (App $app) {
$app->post('/hello', function (Request $request, Response $response) {
$response->getBody()->write('Hello world!');
return $response;
});
};
and the index.php as
<?php
declare(strict_types=1);
use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
require __DIR__ . '/../vendor/autoload.php';
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
if (false) { // Should be set to true in production
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}
// Set up settings
$settings = require __DIR__ . '/../app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/../app/dependencies.php';
$dependencies($containerBuilder);
// Set up repositories
$repositories = require __DIR__ . '/../app/repositories.php';
$repositories($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$callableResolver = $app->getCallableResolver();
// Register middleware
$middleware = require __DIR__ . '/../app/middleware.php';
$middleware($app);
// Register routes
$routes = require __DIR__ . '/../app/routes.php';
$routes($app);
/** #var bool $displayErrorDetails */
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];
// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();
// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
// Create Shutdown Handler
$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails);
register_shutdown_function($shutdownHandler);
// Add Routing Middleware
$app->addRoutingMiddleware();
// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
// Run App & Emit Response
$response = $app->handle($request);
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
Attempts to access post route as 127.0.0.1/appdb/public/hello/Peter, gives an error
{ "statusCode": 404, "error": { "type": "RESOURCE_NOT_FOUND", "description": "Not found." } }
What am I doing wrong?
Thanks in advance

slim exception not sure whats going wrong

I'm using slim 4, and the URL im using is http://localhost/MyApi/public/hello/WeAreLearningPHP
Details
Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php
Line: 91
Trace
0 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.php(57): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
1 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
2 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/selective/basepath/src/BasePathMiddleware.php(52): class#anonymous->handle(Object(Slim\Psr7\Request))
3 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Selective\BasePath\BasePathMiddleware->process(Object(Slim\Psr7\Request), Object(class#anonymous))
4 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(89): class#anonymous->handle(Object(Slim\Psr7\Request))
5 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(132): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class#anonymous))
6 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): class#anonymous->handle(Object(Slim\Psr7\Request))
7 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/App.php(208): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
8 /Applications/XAMPP/xamppfiles/htdocs/MyApi/vendor/slim/slim/Slim/App.php(192): Slim\App->handle(Object(Slim\Psr7\Request))
9 /Applications/XAMPP/xamppfiles/htdocs/MyApi/public/index.php(28): Slim\App->run()
10 {main}
----------------index.php-------------------
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Selective\BasePath\BasePathMiddleware;
use Slim\Factory\AppFactory;
require_once __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
// Add Slim routing middleware
$app->addRoutingMiddleware();
// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$app->add(new BasePathMiddleware($app));
$app->addErrorMiddleware(true, true, true);
// Define app routes
$app->get('/', function (Request $request, Response $response) {
$response->getBody()->write('Hello, World!');
return $response;
})->setName('root');
// Run app
$app->run();

Slim\Exception\HttpNotFoundException

I'm creating a new Slim project and getting the following error:
Slim Application Error:
The application could not run because of the following error:
Error Details
Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 91
Trace
#0 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(57): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#2 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): class#anonymous->handle(Object(Slim\Psr7\Request))
#3 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class#anonymous))
#4 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): class#anonymous->handle(Object(Slim\Psr7\Request))
#5 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#6 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(158): Slim\App->handle(Object(Slim\Psr7\Request))
#7 C:\xampp\htdocs\MyApi\public\index.php(18): Slim\App->run()
#8 {main}
Here is my index.php
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require '../vendor/autoload.php';
$app = AppFactory::create();
$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
This might be a basic problem.But I am new.Need help please.
To solve the problem with Slim 4 here is what I did :
1/ Just after
$app = AppFactory::create();
I added :
$app->setBasePath("/myapp/api"); // /myapp/api is the api folder (http://domain/myapp/api)
2/ In my .htaccess :
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^api/(.*)$ api/index.php/$1
3/ Finaly to handle the 'HttpNotFoundException' Slim Exception I added a try/catch (I don't know why Slim doesn't handle it internaly => maybe to give us more possibility?)
try {
$app->run();
} catch (Exception $e) {
// We display a error message
die( json_encode(array("status" => "failed", "message" => "This action is not allowed")));
}
Hope it can help
** Edited => To force Slim to handle Exceptions, after : **
$app = AppFactory::create();
add :
$app->addErrorMiddleware(true, true, true);
Ref : http://www.slimframework.com/docs/v4/middleware/body-parsing.html
This worked for me (based on Mandien's answer).
I've just added index.php in setBasePath(...), no .htaccess needed.
$app->setBasePath("/myapp/public/index.php");
// Define app routes
$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
http://localhost/myapp/public/index.php/hello/world
shows Hello, world
Linux, Apache, Slim 4
I was getting the same error, let me describe what I was exactly doing and then how did it work!
I was running local server using the command on terminal php -S localhost:8002
The command returned Document root is /Users/Codeus/Desktop/ and error occured when I navigated to localhost:8002
Then again I ran localhost from the directory where index.php or whatever your is that you wanna run and it worked fine for me. For example, for me, it was two levels up then command php -S localhost:8002 returned Document root is /Users/Codeus/Desktop/myslim/src/public and this is exactly where my index.php file was. Hope this works for you as well.
If you are using slim and a newbie then try this code and it will work
Simply change your code a little.
Right now, you all have this code:
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) { ... }
Change it to full naming convention:
$app->get('/[my-appname]/public/hello/{name}', function (Request $request, Response $response, array $args) { .. }
Full code
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
//here i used the url change to your project name on [my-appname]
$app->get('/[my-appname]/public/hello/{name}', function (Request $request,
Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
After nearly 3 hours of intense browsing, I have come up with the solution which would be to downgrade the version of Slim framework from the project's root directory.
I was using v4.3.0 which due to some strange reasons didn't want to behave the way it is meant to and the resolution for which was beyond my understanding.
The steps would be as follows:
Go to the project root directory and type cmd in the file path for
the command prompt window.
Type in composer require slim/slim:3.*
Once done, you would then need to replace the pre-existing code on
public/index.php from your code editor with the below:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
//use Slim\Factory\AppFactory;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
The above snippet is basically the intro page code but from Slim v3.12.3

Add log files to slim framework

I am completelly new to Slim. I have used php for the last 3-4 years but I have always done everything from scratch. I want to learn this frameworks for some rest services I have to do.
I have followed a tutorial on the slim webpage to get a simple rest service working but I want to add a log system to see what is happening when something goes wrong or wathever.
This is what I have right know:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../Slim/Slim.php';
\Slim\Slim::requisterAutoloader();
$application = new \Slim\App();
$logger = $application->log;
$logger->setEnabled(true);
$logger->setLevel(\Slim\Log::DEBUG);
$application->get(
'/hello/user',
function ()
{
GLOBAL $logger;
$logger->info("starting the handling function");
echo "<data>response</data>";
$logger->info("ending handling function");
});
$application->run();
?>
I also tried with monolog but I didn't get it working.
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
require '../vendor/autoload.php';
$logger = new \Flynsarmy\SlimMonolog\Log\MonologWriter(array(
'handlers' => array(
new \Monolog\Handler\StreamHandler('./logs/'.date('Y-m-d').'.log'),
),
));
$app = new Slim\App(array(
'log.writer' => $logger,
));
$app->get('/hello/{name}', function (Request $request, Response $response){
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
//$app->log->writer("hola");
$this->logger->info("Slim-Skeleton '/' route");
return $response;
});
$app->run();
What I would really want is to have a daily log with warnings, debug, info... in the same file. Changing the file every day.
The slim-skeleton application shows how to integrate monolog.
Register the logger with the DI container:
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new \Monolog\Logger('test-app');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
return $logger;
};
Use it in your closure:
$app->get('/hello/{name}', function (Request $request, Response $response){
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
$this->get('logger')->info("Slim-Skeleton '/' route");
return $response;
});
The logs will out output to stdout, so you'll see the in your apache error log or if you're using the built-in PHP server, in the terminal.

Categories