Issue
How can I connect cakephp version 4.x to mongoDB. I tried many ways but couldn't. I need help from the community. Thanks
Solution
You can follow below steps
Install the library by running:
composer require mongodb/mongodb
You can create a folder in your src folder
Example you location is :
app/cake/src/Mongo/connection.php
Then you can write below code to connect mongo
<?php namespace App\Mongo; use MongoDB\Client as Mongo; class Mongo { public function connection(){ $user = "admin"; $pwd = 'password'; return new Mongo("mongodb://${user}:${pwd}@127.0.0.1:27017"); ''' }
Use this class in any controller
<?php
use App\Mongo\Mongo;
//In before filter method
$this->mongo = new Mongo;
'''''
Answered By - Alimon Karim
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.