remains status code 500 in slim framework - php

When a install by composer:
/var/www/html/slim# php composer.phar install Loading composer
repositories with package information Installing dependencies
(including require-dev) from lock file Nothing to install or update
Generating autoload files
when i check:
tail -f /var/log/apache2/error.log
Failed opening required 'Slim\Slim.php'
(include_path='.:/usr/share/php:/usr/share/pear')
My code is:
require 'Slim\Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/hoheckell/:nome)',
function($nome = null){
echo 'Hoheckell' . $nome . '<br />';
});
$app->run();
And remains the Status Code:500 Internal Server Error

Searching a bit more I changed the version that was installed, it was the last stable. God sure as it was found Slim.php file.
The documentation is weak in this part.
This code works:
require_once 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/',
function($nome = null){
echo 'Hoheckell: ' . $nome;
});
$app->run();

Related

Calling Flysystem, why do I get PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found?

I try to run Flysystem's basic example code for the Local adapter and get a Class 'League\Flysystem\Adapter\Local' not found error. This is my process:
version check:
php -v
PHP 5.5.9-1ubuntu4.23 (cli) (built: Feb 8 2018 21:59:47)
install Flysystem:
composer require league/flysystem
output shows I'm up-to-date (this is my 2nd time running it):
Using version ^1.0 for league/flysystem
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
now there's a vendor folder in web root. And within ./web/vendor/league/flysystem/src/Adapter$ are these files:
AbstractAdapter.php
AbstractFtpAdapter.php
CanOverwriteFiles.php
Ftpd.php
Ftp.php
Local.php
NullAdapter.php
Polyfill/
SynologyFtp.php
...just showing that it seems to be installed correctly(?) I create one test file and one test directory in my web root:
fly-local.php
myfiles/
Into fly-local.php I paste the text from their docs (https://flysystem.thephpleague.com/docs/adapter/local/):
<?php
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
...and change the adapter's root folder to myfiles (is that correct?). Then I run it:
php fly-local.php
It outputs:
PHP Fatal error: Class 'League\Flysystem\Adapter\Local' not found in /[PROJECT DIR]/web/fly-local.php on line 6
PHP Stack trace:
PHP 1. {main}() /[PROJECT DIR]/web/fly-local.php:0
What am I doing wrong?
You used composer, then you need to include the composer autoload.php file.
The fly-local.php should be:
<?php
require __DIR__.'/vendor/autoload.php';
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
$adapter = new League\Flysystem\Adapter\Local(__DIR__.'/myfiles');
$filesystem = new Filesystem($adapter);
If you use a framework, you can see it includes the autoload php file for you (index.php, in general). If your test/custom file is not included to framework, you need to include the file manually.

Class 'Google\Cloud\Storage\StorageClient' not found

use Google\Cloud\Storage\StorageClient;
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
That as my code.Here I have installed composer on windows and getting following error:-
Fatal error: Class 'Google\Cloud\StorageClient' not found in C:\xampp\htdocs\fingertips\application\controllers\teacher.php on line 214
And even after running commands with composer to use google cloud api's, then also nothing is happening.
On cmd, when I am running this, "composer require google/cloud-storage" ,I am getting this
Using version ^1.3 for google/cloud-storage
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I have run so many commands to fix this but didnt get anything in success.Can somebody please help what went wrong
In regards to your second question, you didn't include the actual error you're seeing.
I can see an issue with this code block though:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$file = fopen($params['book']['tmp_name'], 'r');
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload($params['book']['name'], [
'name' => 'test.pdf'
]);
I'm missing the actual data you want to upload. The upload method needs data to upload. This should work:
require __DIR__ . '\vendor\autoload.php';
$storage = new StorageClient();
$bucket = $storage->bucket('fingertips-books');
$object = $bucket->upload(file_get_contents($params['book']['tmp_name']), [
'name' => 'test.pdf'
]);
See the documentation for more examples of how to go about uploading a file.
Have you installed storage client? as stated here.
https://packagist.org/packages/google/cloud-storage
use
composer require google/cloud-storage

class 'MongoDB\Cient' not found

I am using the following code to create a connection to a MongoDB server, so as to create databases and stuff but I keep getting class 'MongoDB\Cient' not found. I really need help.
<?php
$client = new MongoDB\Client('mongodb://localhost:27017');
$db = $client->reach;
$collection = $db->messages;
$collection->insertOne($record);
?>
where $record is some record.
Also, $mng = new MongoDB\Driver\Manager("mongodb://localhost:27017"); connects and I can use it to communicated with any already created db (using the command line). So, I want to ask if MongoDB\Driver\Manager can substitute MongoDB\Client?
After all the struggle, this solved the issue...
installing using composer:
$ composer require "mongodb/mongodb=^1.0.0"
OUTPUT:
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Installing mongodb/mongodb (1.0.0)
Downloading: 100%
Writing lock file
Generating autoload files
Usage:
<?php
require 'vendor/autoload.php'; // include Composer goodies
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
?>
That fixed the issue for me. follow the link:
http://docs.php.net/manual/en/mongodb.tutorial.library.php

Installing Composer with autoload.php and use Aws\* says Class not found

I'm running my php in cli under my user.
I have installed composer and autoloader.php does exist. Under vendor folders and file have been downloaded (autoload.php aws, bin, composer, guzzlehttp, mtdowling and psr)
Now in my php code I do:
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;
$credentials = new Credentials('KEY', 'SECRETKEY');
$client = Route53Client::factory(array(
'credentials' => $credentials
));
When I run the script I get: PHP Fatal error: Class 'Credentials' not found in /home/user/updatedns.php on line 15
I tried running it a sudo also (in case it needs to write to the directory) and still get this error.
Just found out that use Aws\Common\Credentials\Credentials; is now under use Aws\Credentials\Credentials;

Installing Slim, PHP, Ubuntu

I'm trying to install Slim on my local LAMP server on Ubuntu only I get stuck at second base.
From my tutorial and various documentation found online:
You now have access to the composer command. Sure enough if I go to
the terminal and enter:
$ composer
Composer version b474944155429eb4cce186c746d55287ee6bb3f4
Usage:
[options] command [arguments]
The next step is to specify Slim as a required package for your app.
This can be accomplished, via a composer.json file within the root of
your project.
Where is the root of my project? I thought it would be
/var/www/slim
I've tried adding composer.json to:
/var/www/slim
and stood in /slim define an index.php script with:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
?>
Go to
http://localhost/var/www/slim
and the browser returns:
Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/slim/index.php on line 2
Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/slim/index.php on line 2
Well, there seems to be more then one question in your post, but regardless ...
"Where is the root of my project?"
The root of your project should be in /var/www if you haven't changed your hosts / Apache settings.
Then on to your 2nd question, which I will take the liberty to rephrase :)
"How to create your web App, and include the composer-installed packages inside?"
go to your respective web-root directory, likely in your case /var/www and in it create "index.php". Then, there, in the console, run the command:
composer install
This should install the packages defined in your composer.json, which should in the same web root dir.
If everything went OO, you will have in the following a new directory: /var/www/vendor
Now, go to your web-root directory and create your index.php and in it, in the beginning add the following lines:
require 'vendor/autoload.php';
// to load all the packages installed with composer, not only slim. If
//composer was run in the right directory and w/o problems of course :)
$app = new \Slim\Slim();
// to instantiate Slim script instance and assign it to the pointer $app
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
})
//to create your 1st route ...
you need to run
composer install
from terminal. After that, add
$app->run();
in index.php.

Categories