Illuminate library capsule object not accepting 'addConnection' command

随声附和 提交于 2019-12-12 04:06:43

问题


I am developing an application in Slim framework using Illuminate library. I am trying to create a connection to the database using Illuminate\Database\Capsule\Manager. Some how, it isnt recognizing the 'addConnection' statement. Here is my full code. If anyone can point out where I am going wrong, that would be very helpful.

require 'lib/vendor/PHPMailer/PHPMailerAutoload.php';
require 'lib/init.php';
require 'lib/Slim/Slim.php';

date_default_timezone_set('UTC');

use lib\Slim\Middleware\SessionCookie;

\Slim\Slim::registerAutoloader();


use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;


$capsule->addConnection(array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'mydatabase',
    'username'  => 'myusername',
    'password'  => 'mypassword',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
));

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

$app->run();

This is where the script stops and the application goes into 'not responding' state.

$capsule->addConnection(array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'mydatabase', 'username' => 'myusername', 'password' => 'mypassword', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ));

I have checked my database details over and over again and they are 100% correct. Can anyone point out what might be causing it?

UPDATE:

After testing, I have come to realize that the page is giving error http 500 on any wrong statement. For example, if I forget to put a semicolon at the end of the statement, it goes into 500. This suggests that the error might be something else other than SQL. Is there any way to enable debugging so it shows me the cause of error. I might be able to track it down from there?

UPDATE

I have enabled debugging. It gives me the following error:

Call to undefined function Illuminate\Support\value()


回答1:


"goes into 'not responding' state." this seems more like 'not responding' from MySQL server. I would vote for invalid hostname. Try changing localhost with 127.0.0.1. Also (if supported) it is better to use mysqli (instead of mysql driver).

Because if it freezes for ~30-60 seconds, it is timeout settings before you get any timeout error. Do you get any?

Also, for 'fresh' examples of Eloquent usage, you can find here: https://github.com/mattstauffer/Torch (if you are using laravel 4.2 you should switch branch to 4.2 for older examples)




回答2:


Probably you are missing to include Illuminate\Support\helpers.php where the function value() is defined as this

if (! function_exists('value')) {
    /**
     * Return the default value of the given value.
     *
     * @param  mixed  $value
     * @return mixed
     */
    function value($value)
    {
        return $value instanceof Closure ? $value() : $value;
    }
}


来源:https://stackoverflow.com/questions/36967860/illuminate-library-capsule-object-not-accepting-addconnection-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!