I am using slim framework 2 with medoo via composer, i am making singleton for medoo but when i call the medoo class to configure my db info, so it gives me the fatal error like below
Fatal error: Class 'medoo' not found in C:\xampp\htdocs\school\s.php
on line 5
below is my s.php file
<?php
require 'vendor/autoload.php';
$app = new\Slim\Slim();
$app->container->singleton('test',function () use ($app) {
return new medoo([
'database_type' =>'mysql',
'database_name' =>'mydb',
'server'=> 'localhost',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'option' => [
PDO::ATTR_CASE=>PDO::CASE_NATURAL
]
]);
});
$app->get('/', function () use($app) {
echo "<center><b><a href='#' target='_blank' >WELCOME TO TESTING PAGE</a></b></center>";
$sth = $app->test->insert("t", ["id" =>1, "name" => "dsfdsf"]);
var_dump($sth);
});
$app->run();
?>
If i check the composer.json file then i find slim and medoo both are there, i am not getting why this fatal error is coming please help me
Two things:
you need to import the class
your class name should be case-sensitive
That is:
<?php
use Medoo\Medoo;
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->container->singleton('test',function () use ($app) {
return new Medoo([
// ...
]);
});
For reference, see:
http://php.net/manual/en/language.namespaces.php
https://github.com/catfan/Medoo/tree/v1.4.5
Related
I am trying to use Laravel queue package outside of Laravel with the database driver, but I got an error with resolving namespaces.
Value of $concrete is 'db'.
$reflector = new ReflectionClass($concrete);
Exception message:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class db does not exist' in ..vendor\illuminate\container\Container.php on line 736
ReflectionException: Class db does not exist in ..vendor\illuminate\container\Container.php on line 736
I am using the illuminate DB package also, and it is working fine, but this one throws exception.
Need resolve DB connection manually and add this connection to Queue manager
Grab mysql connection from DB manager:
$connection = Capsule::schema()->getConnection();
$container = new Container();
Grab Queue manager instance:
$manager = $queue->getQueueManager();
Resolve connection:
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
Add connection to Queue manager:
$manager->addConnector('database', function () use ($resolver) {
return new DatabaseConnector($resolver);
});
Done!
Also need add Illuminate Encryptionn package
Complete code:
<?php
/**
* Created by PhpStorm.
* User: Dmitry
* Date: 05.08.2015
* Time: 19:32
*/
require_once "../vendor/autoload.php";
require_once '../db/db_inc.php'; // DB Capsule config
use Illuminate\Container\Container;
use Illuminate\Queue\Capsule\Manager as Queue;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Queue\Connectors\DatabaseConnector;
$connection = Capsule::schema()->getConnection();
$queue = new Queue();
$queue->addConnection([
'driver' => 'database',
'table' => 'jobs', // Required for database connection
'connection' => 'default',
'host' => 'localhost',
'queue' => 'default',
]);
$queue->getContainer()->bind('encrypter', function() {
return new \Illuminate\Encryption\Encrypter('1111111111111111');
});
$queue->getContainer()->bind('request', function() {
return new \Illuminate\Http\Request();
});
$manager = $queue->getQueueManager();
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
$manager->addConnector('database', function () use ($resolver) {
return new DatabaseConnector($resolver);
});
$queue->setAsGlobal();
Queue::push('SomeJobClass', ['parameters']);
As a total php and phalcon newbie, I'm trying to use the recommended universal class loader using this code:
$loader = new \Phalcon\Loader();
// Register some directories
$loader->registerDirs(
array(
"php/assistants/"
)
);
// register autoloader
$loader->register();
$test = new dbAssistant();
as I understand I have to reffer to the php file as a class what I have inside of php/assistants/dbAssistant.php is the following code, trying to connect to a database:
<?php
function connect() {
$connection = new Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => 'localhost',
'username' => 'root',
'password' => 'tt',
'dbname' => 'testdb',
'port' => '3306'
));
echo 'Connected!!!';
}
again what I understand is that I have to refer to dbAssistant.php as a class and that's why I'm using $test = new dbAssistant();, but it gives me the following error:
Fatal error: Class 'dbAssistant' not found in /var/www/html/test/test.php on line 18
I know that it seeme normal, but the strange thing is that if I remove the connect() function and place the code out of it, I can see the Connected!!! echo, but it is followed by the same(above) error. I know that I'm missing something really small here, but as a complete php newbie, I really can't spot the problem.
Can you give me a push?
php/assistants/dbAssistant.php is not a class but a plain Php file. There should be a class in there with the name dbAssistant.
class dbConnect {
public function connect() {
///Do your stuff
}
}
I am trying to get this phalconphp OAuth2.0 wrapper working on my OAuth2.0 server.
The README of this repository is not very clear on how to use the namespaces.
I have followed the guide but I keep getting the following error :
Fatal error: Class 'Sum\Oauth2\Server\Storage\Pdo\Mysql\Client'
not found in C:\localhost\oauth2-phalcon\public\index.php on line 56
Here is my index.php file :
<?php
require __DIR__."/../vendor/autoload.php";
// Setup IIS Rewrite Rules
// Enable the verbs GET,PUT,POST,DELETE
// Check URL Scan for dissallowed seperators eg ; :
$config = new \Phalcon\Config([
'database' => [
'oauth' => [
'host' => 'localhost\test',
'port' => 1433,
'instance' => 'INSTANCENAME',
'username' => 'test',
'password' => 'test',
'dbname' => 'oauth',
'pdoType' => 'sqlsrv',
'dialectClass' => '\Twm\Db\Dialect\Mssql'
],
],
# ...
]);
# Register The Lib to the loader
$loader = new \Phalcon\Loader();
$loader->registerNamespaces([
"Twm\Db\Adapter\Pdo" => "../app/library/db/adapter/",
"Twm\Db\Dialect" => "../app/library/db/dialect/",
"League" => "../vendor/league/oauth2-server/src/League/OAuth2/Server",
//"Sum\Oauth2\Server\Storage\Pdo" => "../Oauth2/Server/Storage/Pdo/Mysql",
"Sum" => "../Oauth2/Server/Storage/Pdo/Mysql",
//"Sum\Oauth2\Server\Storage\Pdo\Mysql" => "../Oauth2/Server/Storage/Pdo/Mysql "
# ...
])->register();
$app = new \Phalcon\Mvc\Micro();
# set as service
$app->setService('oauth', function() use ($config) {
// HERE! We use our custom MSSQL Adapter
//$oauthdb = new Phalcon\Db\Adapter\Pdo\Mysql($config->database->oauth->toArray());
$oauthdb = new \Twm\Db\Adapter\Pdo\Mssql($config->database->oauth->toArray());
$server = new \League\OAuth2\Server\Authorization(
new \Sum\Oauth2\Server\Storage\Pdo\Mysql\Client($oauthdb),
new Sum\Oauth2\Server\Storage\Pdo\Mysql\Session($oauthdb),
new Sum\Oauth2\Server\Storage\Pdo\Mysql\Scope($oauthdb)
// new \Sum\Oauth2\Server\Client($oauthdb),
//new \Sum\Oauth2\Server\Session($oauthdb),
//new \Sum\Oauth2\Server\Scope($oauthdb)
);
# Not required as it called directly from original code
# $request = new \League\OAuth2\Server\Util\Request();
# add these 2 lines code if you want to use my own Request otherwise comment it
$request = new \Sum\Oauth2\Server\Storage\Pdo\Mysql\Request();
$server->setRequest($request);
$server->setAccessTokenTTL(86400);
$server->addGrantType(new League\OAuth2\Server\Grant\ClientCredentials());
});
$app->get('/hello', function() use($world){
$world = "world";
echo "hello {$world}:)";
});
$app->get('/access', function () use ($app) {
try {
$params = $app->oauth->getParam(array('client_id', 'client_secret'));
echo json_encode(
$app->oauth
->getGrantType('client_credentials')
->completeFlow($params)
);
} catch (\League\OAuth2\Server\Exception\ClientException $e) {
echo $e->getTraceAsString();
} catch (\Exception $e) {
echo $e->getTraceAsString();
}
});
$app->handle();
//echo $app->handle()->getContent();
The project repository for the phalcon wrapper is here :
https://github.com/sumeko/phalcon-oauth2
I have contacted the author already but he is not replying to my emails.
I appreciate any help or advice, thanks.
UPDATE
So I solved my issue. Basically you need to have the standard OAuth2 library installed via composer and the phalconphp OAuth2 wrapper.
That solved it :)
This might be a long shot, but the problem might be with the autoloader that you are explicitly defining. If you use composer's autoload, you don't need to include Sum namespace in Phalcon's loader. Remove all vendor-specific paths from $loader->registerNamespaces() and only use require __DIR__ . "/../vendor/autoload.php" for that.
Also, it's often more convenient use composer's autoloader for your internal things too, e.g.:
{
"require": {
"phpunit/dbunit": "*",
"phpunit/phpunit": "*",
"…": "…"
},
"autoload": {
"psr-0": {
"": "../src"
}
}
}
So I solved my issue. Basically you need to have the standard OAuth2 library installed via composer and the phalconphp OAuth2 wrapper. That solved it :)
I have installed Sentry with Composer, thus it has installed Illuminate Database too. The installation was successful, I have done everything in the Sentry documentation. I'm trying to add a user to the database with a simple code. However it gives me this error message:
Fatal error: Call to a member function connection() on a non-object in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\ilhan\vendor\illuminate\database\Illuminate\Database\Eloquent\Model.php on line 2472
My code is as follows:
<?php
include_once "vendor/autoload.php";
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'ilhantestdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]);
use Cartalyst\Sentry\Sentry as Sentry;
try
{
$user = new Sentry;
// Create the user
$user->createUser(array(
'email' => 'john.doe#example.com',
'password' => 'test',
'activated' => true,
));
// Find the group using the group id
$adminGroup = Sentry::findGroupById(1);
// Assign the group to the user
$user->addGroup($adminGroup);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
echo 'User with this login already exists.';
}
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
{
echo 'Group was not found.';
}
Even I don't know how to debug this. Also, I thought that since Illuminate comes with Sentry, Sentry should be coded how to handle Illuminate thus I don't need much configuration. The documentation is poor and I was unable to find what to do with this error.
You must also boot the ORM. Documentation is not really clear about this.
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
...
]);
$capsule->bootEloquent();
Try setting Capsule as a global. I can't speak to Sentry, but I'm trying to use Capsule on a project, myself, and am running into this very same issue.
Take a look here: https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L113 You'll see that a lot of the convenience functions in there depend on having the static::$instance variable set, which gets set on https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L192.
In my case, I'm trying to use $capsule without setting it as a global. What I ended needing to do was to write my query like so $capsule->getConnection()->table('foo')->get(). In your case, I think Sentry is attempting to access Eloquent and Capsule through static class methods.
tl;dr Run $capsule->setAsGlobal();
I created a simple Phalcon project using Phalcon DevTools (1.2.3).
Now I want to use MongoDB for the database. How do I set this up correctly?
I came this far (see code below):
This is my config.php
<?php
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Nosql', //Was 'Mysql', but Nosql is not supported?
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'test',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'baseUri' => 'localhost/',
)
));
This is my services.php
<?php
use Phalcon\DI\FactoryDefault,
Phalcon\Mvc\View,
Phalcon\Mvc\Url as UrlResolver,
//Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter, //Do I need this when I use Mongo?
Phalcon\Mvc\View\Engine\Volt as VoltEngine,
Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter,
Phalcon\Session\Adapter\Files as SessionAdapter;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function() use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->set('view', function() use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('mongo', function() use ($config) {
$mongo = new Mongo();
return $mongo->selectDb($config->database->dbname);
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function() {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
I altered the standard mysql db connection to be mongo, using the documentation.
But now I have to set up my database adapter in Config, but Nosql doesn't seem to work. DevTools throws this error in the terminal when trying to create a model:
Error: Adapter Nosql is not supported
When I do put in ' Mysql' for the adapter in the config and try to create a model, this is the error I get:
Error: SQLSTATE[HY000] [2002] No such file or directory
Does it need the Mysql adapter to be set in order to use Mongo/Nosql? Or should I put in something else for the adapter/config? Any ideas?
within service.php file where you have mongo service registered
$di->set('mongo', function() {
$mongo = new Mongo();
return $mongo->selectDb("DB_NAME");
}, true);
below that put following lines of code,
$di->set('collectionManager', function(){
return new Phalcon\Mvc\Collection\Manager();
}, true);
After the above is done you need to ensure that your model is extending from \Phalcon\Mvc\Collection
E.g. Customers.php
class Customers extends \Phalcon\Mvc\Collection
{
public $name;
public $email;
}
Once above is done, you can verify if everything is working fine as follows
$robot = new Customers();
$robot->email= "abc#test.com";
$robot->name = "XYZ";
if ($robot->save() == false)
{
echo "Could not be Saved!!";
}
else
{
echo "Data Saved!!";
}
You can put above code in any Controller-Action and try.
If everything goes well, you should be able to see one document created within Customer collection within your database of MongoDB.
Refer http://docs.phalconphp.com/en/latest/reference/odm.html for more..
$di->set('mongo', function() {
$user = 'blog';
$password = 'blog';
$host = 'localhost';
$port = '27017';
$db = 'blog';
$cn = sprintf('mongodb://%s:%d/%s',$host,$port,$db);
$con = new Mongo($cn,array('username'=>$user,'password'=>$password));
return $con->selectDB($db);
}, true);
As of the latest PhalconPHP, MongoDB seems to be a supported option for cache only, not for using as a replacement for the database.
It looks there is no MongoDB adapter for the Config component, only MySQL and couple of others (text based) in the incubator (https://github.com/phalcon/incubator).
You can still use MongoDB for the ACL and your Models though, see https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Acl/Adapter and http://docs.phalconphp.com/en/latest/reference/odm.html#