Get Fatal error when installing slim 3 framework - php

I was installing slim 3 according to Slim Documentation.After installing i have created index.php file.Given below:
index.php:
<?php
require 'vendor/autoload.php';
$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
$response->write("Hello, " . $args['name']);
return $response;
});
$app->run();
But when i run the application in http://localhost:8000 then i get a fatal error.I have searched and get some solution in stackoverflow.Those are:
PHP Fatal error: Class 'Slim' not found - Slim Framework 3
PHP Fatal error: Class 'Slim' not found
Composer autoloader + slim framework - fatal error: Class 'Slim\Slim' not found?
But those solution does not solve my problem.Have any specific solution of it?
ERROR:
Fatal error: Class 'Slim\App' not found in
C:\xampp\htdocs\api\index.php on line 5
And my directory format:
UPDATED:
vendor/autoload.php:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit98cfb2e091de2f633f87c81d16402aec::getLoader();
vendor forlder:

The problem is that you have downloaded and extracted Slim manually into htdocs\api\Slim.
It wasn't fetched by Composer, then it would reside in the vendor folder (vendor/slim/slim) and the autoload would work automatically.
Add Slim to your composer.json and run composer install again:
{
"require": {
"slim/slim": "^3.0"
}
}
You already require the Composer Autoloader, so the depenendency should be found and loaded after it was fetched.
Referencing: http://docs.slimframework.com/start/get-started/

Related

Fatal error: Uncaught Error: Call to undefined function Dotenv\Dotenv()

I am trying to build a website to practice a code where. I am finding a strange error
Which I am unable to replicate how to solve the issue.
I have followed the steps.
I already downloaded the Vendor by Composer and here is the code I have used. Then I installed last pacjage.
composer require vlucas/phpdotenv
.env
APP_URL=http://oxbir.me
APP_ENV=local
APP_NAME="ACSE E-Commerce Store"
index.php
<?php
require_once __DIR__.'/../bootstrap/init.php';
$app_name = getenv('APP_NAME');
echo $app_name;
init.php
<?php
require_once __DIR__.'/../app/config/_env.php';
_env.php
<?php
define('BASE_PATH', realpath(__DIR__.'/../../'));
require_once __DIR__.'/../../vendor/autoload.php';
$dotEnv = Dotenv\Dotenv(BASE_PATH);
$dotEnv->load();

MVC using composer autoload psr-4 is not worked for me

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
<b>Fatal error</b>: Uncaught Error: Class 'App\Core\Main' not found in /var/www/html/php-framework/index.php:20
Stack trace:
#0 {main}
thrown in <b>/var/www/html/php-framework/index.php</b> on line <b>20</b><br />
My Structure:
Php-framework
-> src
-> Core
-> Main.php
-> vendor
-> composer.json
-> index.php
composer.json file
"psr-4": {
"App\\":"src/"
}
Main.php file
namespace App\Core;
Class Main{
public static function run() {
index.php file
require __DIR__ . "/vendor/autoload.php";
App\Core\Main::run();
but it show me error
This is d my first question on stackoverflow
Check your vendor/composer/autoload_psr4.php file, you must have line like
'App\\' => array($baseDir . '/src'),
If you have not this line try composer dump-autoload (https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-)
Yup!!
I have resolved the bug myself. I don't know how, but it's working fine.
I followed the steps below:
Remove vendor folder
Run composer dump-autoload -o

composer can not find classes and load them

I am using a Dropbox api package and installed it with Composer.When I try to use classes that gives me a fatal error that can not find classes.
This is my composer.json
{
"require": {
"kunalvarma05/dropbox-php-sdk": "^0.2.1"
}
}
This is my php file
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
$app = new DropboxApp("client_id", "client_", 'access_token');
//Configure Dropbox service
$dropbox = new Dropbox($app);
//Get File Metadata
$fileMetadata = $dropbox->getMetadata("/helloworld.txt");
//File Name
// $fileMetadata->getName();
printf($fileMetadata->getName());
My php version is 7.2.4 and the error is:
Fatal error: Uncaught Error: Class 'Kunnu\Dropbox\DropboxApp' not found in D:\MeHDi\Projects\DropBox Api\Upload.php:6
Stack trace:
#0 {main}
thrown in D:\MeHDi\Projects\DropBox Api\Upload.php on line 6
Have you remembered to import composers autoload file using require "path/to/vendor/autoload.php";
This is required in order to initialize and use the different composer libraries. Remember to change the filepath to where your vendor/autoload.php file is located.

Namespace autoloading with composer and PSR-0 not working as expected

I have made a test project to understand how composer and packagist works. The project is also on packagist.
A simple composer require rakibtg/gowin will install the package from packagist.
But for some reason the namespacing is not working as expected.
Here is my directory structure and the composer file.
Here is the GoWin.php file:
<?php
namespace GoWin;
class GoWin {
public function serve() {
echo 'Lets Win Everybody!';
}
}
Here is the test.php file where i am trying to use the serve() method from the GoWin class.
<?php
require_once './vendor/autoload.php';
// use GoWin;
( new GoWin\GoWin() )->serve();
But it fails to execute the serve method with this error:
Fatal error: Uncaught Error: Class 'GoWin\GoWin' not found in
/Users/usr/Desktop/t estGoWin/index.php:7 Stack trace:
0 {main} thrown in /Users/usr/Desktop/testGoWin/index.php on line 7
At this moment i cant understand what i am missing! Also should i use psr-0 or psr-4?
I solved it by switching to PSR-4, simply update the composer.json autolaod property as this:
"autoload": {
"psr-4": {
"GoWin\\": "src/"
}
},

PHPunit in zendframework 2

i'm trying to use phpunit with zendframework and i'm follwing the tutorial in
https://media.readthedocs.org/pdf/zf2/latest/zf2.pdfhere is my
bootstrap.php
<?php
chdir(dirname(__DIR__));
include __DIR__ . '/../init_autoloader.php';
here is my IndexControllerTest.php
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/C:/wamp/www/zf2/config/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/'); // this is line 20
$this->assertResponseStatusCode(200);
$this->assertModule('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
and i'm getting the follwing errors
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
Fatal error: Class 'Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase'
not found in C:\wamp\www\zf2\module\Application\test\ApplicationTest\Controller
\IndexControllerTest.php on line 8
i think that's a path probleme (auloading)but i don't know how to fix
any one can help me please ?
Warning: include(C:\wamp\www\zf2\module\Application\test/../init_autoloader.php)
: failed to open stream: No such file or directory in C:\wamp\www\zf2\module\App
lication\test\Bootstrap.php on line 4
This warning is telling you that it can't find the location of your init_autoloader.php file. Assuming that file is located in the root of your ZF2 project (so C:\wamp\www\zf2) as is convention, you need to change:
include __DIR__ . '/../init_autoloader.php';
to
include __DIR__ . '/../../../init_autoloader.php';
EDIT Continued...
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to
load ZF2. Run php composer.phar install or define a ZF2_PATH environment
variable.' in C:\wamp\www\zf2\init_autoloader.php:48
Your init_autloader.php file is having trouble finding your ZF2 library autoloader. As you're using composer. Add
"zendframework/zendframework": "2.1.*",
to your "require" section in composer.json if its not already there. Run composer and update your vendor libraries with
php composer.phar update
Try run the application again and see if it works. It may do depending what is included in your init_autoload.php file. If you're still having problems add the following to init_autoloader.php
if(file_exists('vendor/autoload.php'))
{
$loader = require 'vendor/autoload.php';
}
Here's the Fix for the fatal error.
You must be missing the 'zend-test' package in your application.
$ composer require zendframework/zend-test

Categories