Calling OpenCart library functions in a PHP file outside of the framework

你。 提交于 2019-12-03 15:06:05

Instead of doing this:

include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.php');

(including the user class only), include the startup.php that will load all the system classes from OpenCart (may be unwanted or may collide with the scope of project where you need to use those OpenCart classes, but this could be fixed when problems occur). You would need to include the OpenCart's config.php as well to make sure that OpenCart's DB class will have the required constants for connecting to DB defined. You may want to do it in clear way by checking whether the files exist first:

$root = $_SERVER['DOCUMENT_ROOT'] . '/mydir/opencart/';

if (file_exists($root . 'config.php')) {
    require_once($root . 'config.php');
}
if (file_exists($root . 'system/startup.php')) {
    require_once($root . 'system/startup.php');
}
if (file_exists($root . 'system/library/user.php')) {
    require_once($root . 'system/library/user.php');
}

Then you can try to login your user:

$user = new User();
if ($user->login('username','password')) {
    echo 'User was logged in successfully';
} else {
    echo 'User not found or username or password do not match.';
}

That would be for a code example.

Pleaser, take a look into admin/index.php how all the required classes are instantiated prior to creating the User object.

$registry = new Registry();

$loader = new Loader($registry);
$registry->set('load', $loader);

$config = new Config();
$registry->set('config', $config);

$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

// ... do the same for rest of required classes ...

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