Laravel 4: Class 'MongoClient' Not Found - php

I have Laravel 4 installed on WAMP and it works great with a MySQL backend.
I have successfully setup second virtual host and would like to use a MongoDB backend. After searching around I found out that Laravel does not natively connect to MongoDB and I found https://github.com/jenssegers/Laravel-MongoDB and I have been trying to set it up but I can't seem to get it right. Obviously I must be doing something wrong and I am hoping someone can help me identify what it is that I am not doing right.
I edited composer.json per the instructions:
............
"license": "MIT",
"require": {
"laravel/framework": "4.1.*",
"jenssegers/mongodb": "*"
},
"autoload": {
.........
Then I ran composer update. It installed monolog 1.9.1 and swiftmailer v5.2.0 - whatever these are - successfully (a few days ago) but then threw an error after that. Today I tried to run composer update again, and it updated the two to 1.10.0 and v5.2.1 respectively and then encountered the same error. Now when I try composer update it consistently throws the same error:
Nothing to install or update
Generating autoload files
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","me
ssage":"Class 'MongoClient' not found","file":"C:\\wamp\\www\\laravel\\vendor\\j
enssegers\\mongodb\\src\\Jenssegers\\Mongodb\\Connection.php","line":132}}Script
php artisan clear-compiled handling the post-update-cmd event returned with an
error
[RuntimeException]
Error Output:
update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock]
[--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [--with-
dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [packages1] ... [
packagesN]
What I have tried:
I have downloaded and installed the php_mongo.dll by placing it in the php ext folder and enabling it in php.ini:
; added for mongoDB connections
extension=php_mongo.dll
But this did not help.

NOTE: I would not have been able to resolve the issue completely without #Hassan 's help -- please see comments under #Hassan 's answer.
I'll provide this answer in the hopes that it may help someone else who may experience the same issue. I thought it better to give it as an answer so that it stands out.
Further search lead me here: https://github.com/jenssegers/Laravel-MongoDB/issues/36
Then here: https://github.com/leroy-merlin-br/mongolid-laravel#troubleshooting
The following command and output indicates the location of php.ini that I should have updated with the php_mongo.dll extension:
$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => C:\Windows
Loaded Configuration File => C:\wamp\bin\php\php5.5.12\php.ini
The WAMP tray icon however points to C:\wamp\bin\apache2.4.9\bin\php.ini -- which is what I had updated. I also checked if PHP in the CLI environment is importing the driver properly by running the following command with the output shown:
$ php -i | grep 'Mongo'
MongoDB Support => enabled
After updating the correct php.ini, I restarted apache and tried again composer update again. The error was different -- authentication -- as the username, password and database were wrong. Once those were corrected, the update completed without incident.

Your composer file looks fine, as is probably everything else. Try a "composer dumpautoload", from the docs:
If you need to update the autoloader because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.
This should fix that error, after which you'll need to change your adapter in app/config/database.php to use mongodb like so:
'default' => 'mongodb',
And add mongodb to your connections too:
'connections' => array(
...
'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => '',
'password' => '',
'database' => 'test'
),
),

Related

Laravel cannot find public dir

I worked on a team and then clone the laravel/php codes from our repository. When I serve the laravel on localhost, it cannot find all the file inside the public directory and throw an error on the terminal:
[404]: GET /public/css/style.css - No such file or directory
Many answers to similar issues advise me to change the codes in the blade.php file. The problem is that I can't edit the blade.php because it works fine on other team members even though we have the same ubuntu, PHP, and laravel version and .env file.
What I have tried:
Run composer install and composer update
Try different browsers such as firefox and chromium-based browsers
Run php artisan storage:link
Restart apache2
Run npm install and npm run dev
I think there is some package or something missing.
List of paths I get from dd($__data):
"path" => "/project-directory/project-name/app"
"path.base" => "/project-directory/project-name"
"path.lang" => "/project-directory/project-name/resources/lang"
"path.config" => "/project-directory/project-name/config"
"path.public" => "/project-directory/project-name/public"
"path.storage" => "/project-directory/project-name/storage"
"path.database" => "/project-directory/project-name/database"
"path.resources" => "/project-directory/project-name/resources"
"path.bootstrap" => "/project-directory/project-name/bootstrap"
Have you tried?
php artisan storage:link
You need first run
php artisan storage:link
For more details check https://laravel.com/docs/8.x/filesystem
Then you must be check your .env values.
Is correctly APP_URL=
Also in your exception is strange
/public/.../...
Reason is wrong configuration.
First check your .env values like
APP_URL=...
FILESYSTEM_DRIVER=public
then check config/filesystems.php

Class 'Redis' not found in Lumen

Lumen Version: 6.0
PHP Version: 7.2
Database Driver & Version: MySql 5.7, Redis
Code
use Illuminate\Support\Facades\Redis;
Redis::set($key, $data, 'EX', $expiry);
in app.php
$app->withFacades();
$app->withEloquent();
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->configure('database');
Using the above code gives Class 'Redis' not found error. This error occurs only when the below packages are installed.
"illuminate/redis": "^6.5",
"illuminate/mail": "^6.5",
"laravel/lumen-framework": "^6.0",
With below packages which has lower versions it works without any error/issues.
"laravel/lumen-framework": "^5.8.*",
"illuminate/redis": "^5.8",
"illuminate/mail": "^5.8",
So why is it giving error when packages are upgraded.
If you are using Laravel 8, in the database.php file, replace the following line:
'client' => env('REDIS_CLIENT', 'phpredis')
to:
'client' => env('REDIS_CLIENT', 'predis')
then, add the predis dependency with composer:
composer require predis/predis
You can modify config/database.php.
because lumen6 redis default drive used phpredis.
add .env file like this.
REDIS_CLIENT=predis
Make sure you set up the PHP Redis extension and enable it.
Even after you do that, you will need to register an alias for Redis in your app.php file. It's clear that you referenced it with your use statement, but that is only visible in the class you are "using" it. The PHP Redis connector will need to reference it from somewhere globally, which is in the app.php file. Laravel comes with this already set-up, but unfortunately Lumen doesn't.
To be safe, wrap it with a check on class existence.
This is how I fixed the problem.
#You already have this:
$app->register(Illuminate\Redis\RedisServiceProvider::class);
#Add the following right below
if (!class_exists('Redis')) {
class_alias('Illuminate\Support\Facades\Redis', 'Redis');
}
My steps to fix that in Lumen 7:
install "illuminate/redis" package: composer require illuminate/redis:"^7.0"
install "php-redis" on my CentOS7: yum --enablerepo=epel -y install php-pecl-redis
install "predis" package: composer require predis/predis:"^1.0"
change the redis client to "predis" (by default its "phpredis"): 'client' => 'predis'. So the config is:
'redis' => [
'cluster' => false,
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
]

php5enmod mcrypt with Puppet

Another Puppet related question.
As part of my installation with Puppet, I'm installing: -
Ubuntu 14.04.2 LTS
PHP5-FPM
Nginx
MySQL etc
As part of the PHP class I have the following: -
package {[
'php5-fpm',
'php5-mysql',
'php5-cli',
'php5-mcrypt',
'php5-curl',
]:
ensure => present,
require => Exec['apt-get update'],
}
This part works fine. No issues.
Once the server has finished doing its thing, I'm able to run: -
php5enmod mcrypt
This, again, runs without issue and mcrypt is enabled in the php5-fpm installation. The problem is arising with the following code block.
exec { 'enable-mcrypt':
command => 'php5enmod mcrypt',
path => '/usr/sbin',
require => [
Package['php5-mcrypt'],
Package['php5-fpm']
],
notify => [
Service['php5-fpm'],
Service['nginx'],
],
}
I've tried running it in various incarnations, and there are no issues regarding syntax or dependencies for it to execute.
However, when I look through the debug information I'm seeing this: -
Debug: Exec[enable-mcrypt](provider=posix): Executing 'php5enmod pdo'
Debug: Executing 'php5enmod pdo'
Notice: /Stage[main]/Php/Exec[enable-mcrypt]/returns: /usr/sbin/php5enmod: 233: /usr/sbin/php5enmod: expr: not found
Notice: /Stage[main]/Php/Exec[enable-mcrypt]/returns: /usr/sbin/php5query: 181: /usr/sbin/php5query: expr: not found
Notice: /Stage[main]/Php/Exec[enable-mcrypt]/returns: /usr/sbin/php5query: 203: /usr/sbin/php5query: find: not found
Notice: /Stage[main]/Php/Exec[enable-mcrypt]/returns: WARNING:
Notice: /Stage[main]/Php/Exec[enable-mcrypt]/returns: usage: php5enmod [ -s ALL|sapi_name ] module_name [ module_name_2 ]
Error: php5enmod pdo returned 1 instead of one of [0]
Error: /Stage[main]/Php/Exec[enable-mcrypt]/returns: change from notrun to 0 failed: php5enmod pdo returned 1 instead of one of [0]
I cannot make heads nor tails of it. It would almost appear that php5enmod is not seeing the argument that's being passed to it, hence the WARNING: usage php5enmod [ -s ALL|sapi_name ] etc...
I say this because if I run phpenmod without any arguments, that's the same error you get.
If anybody has any ideas, I'd be outrageously thankful.
It appears the correct way to do this (As referenced #BMW's comment) is to ensure that Puppet knows where the "find" command is before attempting to execute php5enmod.
My puppet configuration is below:
# Ensure Mcrypt is enabled
exec { "enablemcrypt":
path => [ "/bin/", "/sbin/" , "/usr/bin/", "/usr/sbin/" ],
command => "php5enmod mcrypt",
notify => Service["apache2"],
require => Package["php5-common"],
}
As you can see, by adding "/bin", "/sbin", "/usr/bin" and "/usr/sbin" to the path parameter, puppet can now use the "find" command, which it seems to use internally when executing commands with arguments. php5enmod now runs correctly for me on Ubuntu 14.04 LTS.
Unfortunately, I wasn't able to get this to work as I would have liked. I'm unsure if Puppet is just not playing nicely with php5enmod, or whether there's some internal issues with php5enmod and the way it's being called by the Puppet scripts.
However, I did manage to manually create the symbolic link and restart the service with the following block of code.
file { '/etc/php5/fpm/conf.d/20-mcrypt.ini':
ensure => 'link',
target => '/etc/php5/mods-available/mcrypt.ini',
require => [
Package['php5-mcrypt'],
Package['php5-fpm'],
],
notify => Service['php5-fpm'],
}
Hopefully this helps somebody out in the future.

Laravel 5 with Postgres SQL

I’m working on Laravel 5 with postgres as database. I’ve configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:
[PDOException]
could not find driver
This is my database.php
'default' => 'pgsql',
'pgsql' => [ 'driver' => 'pgsql',
'host' => '127.0.0.1',
'database' => 'fms',
'username' => 'postgres',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public', ],
Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=fms";
$db = pg_connect( "$host $port $dbname user=postgres password=root" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
I’ve enabled php_pgsql and php_pdo_sql in wamp as well. I’m not sure how to fix this on laravel 5.
As you said you already choosed Default Database as Postgres SQL
'default' => 'pgsql',
It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)
i.e., You need to uncomment the following lines in your php.ini
extension=pdo_pgsql.so
extension=pgsql.so
Note :
Don't forget to stop and start your apache after doing this changes (or php-fpm if using that instead).
I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.
After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.
The solution is (of course) to uncoment the extensions in the following files:
~/wamp/bin/php/php5.5.*/php.ini
~/wamp/bin/php/php5.5.*/phpForApache
~/wamp/bin/php/php5.5.*/php.ini.install
~/wamp/bin/php/php5.5.*/php.ini-development
~/wamp/bin/php/php5.5.*/php.ini-production
and
~/wamp/bin/apache/apache2.4.9/php.ini
** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).
Like #jeff said, this is probably caused by not setting DB_CONNECTION=pgsql in the .env-file. The .env-file has MySQL preconfigured, so you must edit this file.
You have to make DB related changes in
config/database.php
.env file
and other settings in
php.ini settings
If you are still getting error, then clear cache and config
php artisan cache:clear
php artisan config:clear
It should work now!
Run this command to easily uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini
sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini
sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini
If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:
apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql
This will install the pgsql and pdo_pgsql drivers.
you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.
I'm using PHP7.3 and this worked for me, I forgot this:
apt-get install php7.3-pgsql
Everything runs smoothly afterwards. Just use whatever version of PHP you are using.

Way\Generators is breaking Laravel Envoyer

I'm trying to transition a Laravel 4.2 site from Forge to Forge/Envoyer. I'm following the laracast but I keep getting the error:
PHP Fatal error: Class 'Way\Generators\GeneratorsServiceProvider' not
found in
/home/forge/Site/envoyer/releases/20150511192402/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
on line 157
on the Install Composer Dependencies step of Envoyer deployment.
I've removed the lines for Way/Generators from both composer.json and config/app.php and have followed the documentation to re-install it. Envoyer works with Way/Generators removed but keeps failing when I add it back.
Anyone have any ideas on how to fix it?
In composer.json add way/generators inside "require-dev", so it will be downloaded only on your dev machine.
"require-dev": {
"way/generators": "~2.0"
}
Add Way\Generators\GeneratorsServiceProvider only inside your local (development) config - config/local/app.php. That way it will be present on your development machine, because it will use config/local/app.php, but while deploying, envoyer will use config/app.php, where Way\Generators\GeneratorsServiceProvider are not set.
This is how your config/local/app.php can look like:
<?php
return array(
'debug' => true,
'providers' => append_config(array(
'Way\Generators\GeneratorsServiceProvider'
))
);

Categories