Why can "Class 'DI\Bridge\Slim\App'" not be found? PHP - php

I am trying to create a shopping cart for my website, I am following the tutorial below: https://www.youtube.com/watch?v=tRh467FX12U&list=PLfdtiltiRHWH9JN1NBpJRFUhN96KBfPmd&index=2
In the tutorial, he uses 3 different imports from GitHub and this is how installed them in the terminal using composer:
rosscurrie = Name of ~user
#Ross-Air = Name of Macbook
MobileMastersNew = Name of the folder that holds all website files
composer = is installed globally
require <...> = the imports from GitHub
rosscurrie#Rosss-Air MobileMastersNew % composer require slim/slim:^4.0
rosscurrie#Rosss-Air MobileMastersNew % composer require slim/twig-view:^3.0
rosscurrie#Rosss-Air MobileMastersNew % composer require php-di/slim-bridge
rosscurrie#Rosss-Air MobileMastersNew % composer require illuminate/database
I have limited experience with Laravel but not completely unfamiliar. When I try to load the index.php page it gets this error:
Fatal error: Uncaught Error: Class 'DI\Bridge\Slim\App' not found in /Users/rosscurrie/Sites/MobileMastersNew/app/App.php:8 Stack trace: #0 /Users/rosscurrie/Sites/MobileMastersNew/vendor/composer/ClassLoader.php(444): include() #1 /Users/rosscurrie/Sites/MobileMastersNew/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/Users/rosscurr...') #2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Cart\\App') #3 /Users/rosscurrie/Sites/MobileMastersNew/bootstrap/app.php(9): spl_autoload_call('Cart\\App') #4 /Users/rosscurrie/Sites/MobileMastersNew/public/index.php(3): require('/Users/rosscurr...') #5 {main} thrown in /Users/rosscurrie/Sites/MobileMastersNew/app/App.php on line 8
My folder directory is as follows:
My ../MobileMasters/app/App.php is:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
//
}
}
My ../MobileMasters/bootstrap/app.php is:
<?php
session_start();
use Cart\App;
require __DIR__ . '/../vendor/autoload.php';
$app = new App;
My ../MobileMasters/public/.htaccess file is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
My ../MobileMasters/public/index.php is:
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
My ../MobileMasters/vendor/autoload.php is:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit2e9ebb4be0f95ed0dbfb26486b8ba4b7::getLoader();
Lastly, my ../MobileMasters/composer.json is:
{
"require": {
"slim/slim": "^4.0",
"slim/twig-view": "^3.0",
"php-di/slim-bridge": "^3.0",
"illuminate/database": "^7.2"
},
"autoload": {
"psr-4": {
"Cart\\": "app"
}
}
}

You should use use statement after autoloading:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
//
}
}
And change this file also:
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Cart\App;
$app = new App;

You are using wrong namespace use DI\Bridge\Slim\App as DIBridge;. There is no App class in DI package anymore.
Instead
use the following code use DI\Bridge\Slim\Bridge as DIBridge;

Your composer.json needs to look almost exactly like the one in the tutorial. I had the exact same problem. To require the older versions, you just have to do something like composer remove slim/slim then composer require slim/slim ^3.0.
In particular, the your php-di needs to be lower than v1.1. My composer.json:
{
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.1",
"illuminate/database": "^5.2",
"php-di/slim-bridge": "v1.0.2"
},
"autoload": {
"psr-4": {
"Cart\\": "app/"
}
}
}

Related

Class 'app' not found in bootstrap\app.php:4 #1 {main} thrown in C:\Users\Logan\Cart\bootstrap\app.php on line 4

I keep getting this error I checked the composer.json file and everything seems to be alright I'm following a tutorial for a shopping cart online in php. Any help would be greatly appreciated it.
I checked a similar question and he corrected in the composer.json file and that's not the issue with my problem.
bootstrap-app.php
<?php
session_start();
$app = new app;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/routes.php';
index.php
<?php
require __DIR__ . '/../bootstrap/app.php';
$app->run();
composer.json
"autoload": {
"psr-4": {
"Cart\\": "app/"
}
},
I've also have dumped the file before and no changes. I'm new to php and I'm just trying to complete this tutorial so I can complete an e-commerce website.
Fatal error: Uncaught Error: Class 'app' not found in
C:\Users\Logan\Cart\bootstrap\app.php:4 Stack trace: #0 C:\Users\Logan\Cart\public\index.php(3): require() #1 {main} thrown in C:\Users\Logan\Cart\bootstrap\app.php on line 4
Edited to include this:
<?php
namespace Cart;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DIBridge;
class App extends DIBridge
{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefintions (
['settings.displayErrorDetails' => true,] );
$builder->addDefinitions(__DIR__ .'/container.php');
}
}

How in php deployer set host details from .env instead of ~/.ssh/config

I'm looking how I can set up host user and pass from env
this don't work - need string not function
host('51.38.98.87')
->stage('production')
->user( function(){return getenv('PRODUCTION_USER'); )
->pass( function(){return getenv('PRODUCTION_PASS'); )
->port(22)
in Symfony 4 I use env and don't want setup hosts in ~/.ssh/config
Update1 :
<?php
namespace Deployer;
use Symfony\Component\Dotenv\Dotenv;
require 'recipe/symfony.php';
require_once 'vendor/autoload.php';
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
then
dep deploy production
get
dep deploy production
[Error] Class 'Symfony\Component\Dotenv\Dotenv' not found
#0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()
#1 [internal function]: Deployer\Deployer::Deployer\{closure}()
#2 phar:///usr/local/bin/dep/src/Deployer.php(326): call_user_func(Object(Closure))
#3 phar:///usr/local/bin/dep/bin/dep(119): Deployer\Deployer::run('6.3.0',
Update 2
composer.json has symfony/dotenv
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0",
"symfony/browser-kit": "^4.0",
"symfony/css-selector": "^4.0",
"symfony/debug-pack": "*",
"symfony/dotenv": "^4.0",
"symfony/maker-bundle": "^1.0",
"symfony/phpunit-bridge": "^4.0",
"symfony/profiler-pack": "*",
"symfony/web-server-bundle": "^4.0"
},
but still
namespace Deployer;
use Symfony\Component\Dotenv\Dotenv;
require 'vendor/autoload.php';
require 'recipe/symfony.php';
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
give my
Class 'Symfony\Component\Dotenv\Dotenv' not found
#0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()
#1 [internal function]: Deployer\Deployer::Deployer\{closure}()
#2 phar:///usr/local/bin/dep/src/Deployer.php(326): call_user_func(Object(Closure))
#3 phar:///usr/local/bin/dep/bin/dep(119): Deployer\Deployer::run('6.3.0', '/home/grek/Phps...')
#4 /usr/local/bin/dep(4): require('phar:///usr/loc...')
#5 {main}
UPDATE 3
I create test file : ./test.php
<?php
use Symfony\Component\Dotenv\Dotenv;
require 'vendor/autoload.php';
(new Dotenv())->load(__DIR__.'/.env');
php test.php
works fine.
Run from same location dep deploy:staging
give my
[Error] Class 'Symfony\Component\Dotenv\Dotenv' not found
#0 phar:///usr/local/bin/dep/src/Deployer.php(325): require()
my deploy.php file
<?php
namespace Deployer;
use Symfony\Component\Dotenv\Dotenv;
require 'vendor/autoload.php';
require 'recipe/symfony.php';
(new Dotenv())->load(__DIR__.'/.env');
Deployer is installed globaly
whereis dep
dep: /usr/local/bin/dep
Did you tried to enable symfony/dotenv in your deploy.php file?
require_once 'vendor/autoload.php';
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
You need first to install dotenv component:
composer require --dev symfony/dotenv
Remove the --dev option if you use it in an environment with only production dependencies.
I'm using .env files to generate settings files and the way I've got around this was to to add a property to the host that indicates the .env and template files:
# hosts.yml
hostname:
...
env_file: .env.stage
settings_template: somewhere/file.template
# file.template
...
'database' => '{{DB_NAME}}',
...
Load the credentials with a function:
# deploy.php
// Loading of .env files
// add Symfony's dotenv as a requirement: composer global require symfony/dotenv:"^3"
function loadenv() {
$env_file = get('env_file');
$environment = file_get_contents($env_file);
$dotenv = new \Symfony\Component\Dotenv\Dotenv();
$data = $dotenv->parse($environment);
return $data;
};
Finally, create a task that calls the loadenv() function, writes the credentials to a file and uploads it to the server:
task('upload:settings', function () {
if (askConfirmation('Are you sure to generate and upload the settings?')) {
//Get template
$template = get('settings_template');
//Import secrets
$secrets = loadenv();
//Prepare replacement variables
$iterator = new \RecursiveIteratorIterator(
new \RecursiveArrayIterator($secrets)
);
$replacements = [];
foreach ($iterator as $key => $value) {
$keys = [];
for ($i = $iterator->getDepth(); $i > 0; $i --) {
$keys[] = $iterator->getSubIterator($i - 1)->key();
}
$keys[] = $key;
$replacements['{{' . implode('.', $keys) . '}}'] = $value;
}
//Create settings from template
$settings = file_get_contents($template);
$settings = strtr($settings, $replacements);
$tmpFilename = 'settings.tmp';
file_put_contents($tmpFilename, $settings);
upload($tmpFilename, '{{release_path}}/somewhere', ['options'=> ['--inplace']]);
}
});

Fatal error: Class 'eftec\bladeone\BladeOne' not found

I am using PHP 5.5.9 and have installed the library BladeOne in my composer file:
{
"name": "test",
"authors": [
{
"name": "test",
"email": "test#test.com"
}
],
"require": {
"eftec/bladeone": "^3.0",
"davechild/textstatistics": "1.*",
"hassankhan/config": "^1.0"
}
}
I am running my script the following way:
<?php
require "vendor/autoload.php";
Use eftec\bladeone;
use DaveChild\TextStatistics as TS;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
define("BLADEONE_MODE",1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
$blade=new bladeone\BladeOne($views,$cache); <----- Here I get the error!
However, I get the following error here:
Fatal error: Class 'eftec\bladeone\BladeOne' not found in /home/ubuntu/workspace/testExample.php on line 10
Any suggestions why the library cannot be used in my script?
As per the docs you are supposed to add the namespace to the autoloading of composer by adding it to your composer.json.
"autoload": {
"psr-4": {
"eftec\\": "vendor/eftec/"
}
}
then, (again, the docs say) run composer update. I suppose though that composer dump-autoload would suffice.

psr4 autoload class not found

I am making my own framework but I have a problem with psr4 autoload and composer.
I can not make classes available and apparently everything is fine
This is the error, I don't want to use include for class files:
Fatal error: Uncaught Error: Class 'System\Template\get_template_files_class' not found in /var/www/html/fsib/system/layout-compiler.php:7 Stack trace: #0 {main} thrown in /var/www/html/fsib/system/layout-compiler.php on line 7
composer.json
{
"require": {
"illuminate/database": "*",
"illuminate/routing": "~5.1.16",
"illuminate/events": "~5.1.16"
},
"autoload": {
"psr-4": {
"System\\" : "system/libs/"
}
}}
layout-compiler.php
<?php
require_once '../vendor/autoload.php';
use System\Template\get_template_files_class;
$templates = new get_template_files_class;
?>
get_templates_files_class.php
<?php
namespace System\Template
class get_template_files_class {
}
?>

Monolog in Silex application

I try to use Monolog in my Silex application, according to the doc
http://silex.sensiolabs.org/doc/master/providers/monolog.html
I declare MonologServiceProvider in the app.php file as follow:
use Silex\Provider\MonologServiceProvider;
$app->register(new MonologServiceProvider(), array(
'monolog.logfile' => __DIR__ . '/../files/logs/log.log',
));
And I try to write in my log.log file on the controler:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// In a controler
$app['monolog']->info("test");
$app['monolog']->debug("test");
$app['monolog']->warning("test");
$app['monolog']->error("test");
I don't have any error, but it is not working at all.
I just wan't to put my "test" message in my log.log file, how can I do that ?
Thanks for help
I can't really answer your question based on the info you've given, but it's a slow-news day here, so strummed-up a working Silex site with working logging. All the files are on Github, but I'll repeat them here for ease of reading.
composer.json
{
"require" : {
"silex/silex" : "^2.0",
"monolog/monolog" : "^1.0"
},
"autoload" : {
"psr-4" : {
"community\\" : "src/"
}
}
}
public/index.php
<?php
use \community\app\Application;
require_once realpath(__DIR__ . '/../vendor/autoload.php');
$app = new Application();
$app["debug"] = true;
$app->run();
src/app/Application.php
<?php
namespace community\app;
use \Silex\Application as SilexApplication;
use Silex\Provider\MonologServiceProvider;
class Application extends SilexApplication {
function __construct() {
parent::__construct();
$this->registerServices();
$this->mountControllers();
}
function registerServices(){
$this->register(new MonologServiceProvider(), [
"monolog.logfile" => realpath(__DIR__ . "/../../log") . "/general.log"
]);
}
function mountControllers() {
$this->get('/testLog', 'community\controller\TestLogController::doGet');
}
}
src/controller/TestLogController.php
<?php
namespace community\controller;
use community\app\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class TestLogController {
public function doGet(Request $request, Application $app) {
$app["monolog"]->info("hi!");
return new Response("All good", Response::HTTP_OK);
}
}
That writes to log/general.log as follows:
[2016-12-28 13:58:05] app.INFO: hi! [] []
One thing I noticed is that if the path to the log file is bung, then Monolog just seems to swallow it (which is not exactly ideal). This could well be your issue.
Anyway, grab the code above and mess around with it. Hopefully you'll be able to work out the differences between yours and mine, and get yours working.

Categories