如何从CodeIgniter访问Magento?

I have a default CodeIgniter 2.1 install with Magento 10.4.4 installed in a subdir called store.

The following code works when run from web root (with .htaccess disabled). It will give the firstname, lastname of the logged in Magento user.

<?php
$site_root = '/var/www/mysite/www/httpdocs';
require_once ($site_root . '/store/app/Mage.php');
umask(0);
// Initialize Magento and hide sensitive config data below site root
$name='frontend';
$options = array('etc_dir' => realpath('../magento-etc'));
Mage::app('default','store', $options);
Mage::getSingleton("core/session", array("name" => $name));
$websiteId = Mage::app()->getWebsite()->getId();
echo "websiteid: $websiteId<br>";
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setStore($store);
echo 'customerwebsiteId: ' . $customer->website_id . '<br>';
$session = Mage::getSingleton('customer/session');
$magento_message = 'Welcome ';
// Generate a personalize greeting
if($session->isLoggedIn()){
  $magento_message .= $session->getCustomer()->getData('firstname').' ';
  $magento_message .= $session->getCustomer()->getData('lastname').'!';
}else{
  $magento_message .= 'Guest!';
}
echo $magento_message;
?>

But, if I run this in a CodeIgniter model, then isLoggedIn returns false.

Here is the CodeIgniter page:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test_mage extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $site_root = '/var/www/mysite/www/httpdocs';
        require_once ($site_root . '/store/app/Mage.php');
        umask(0);
        // Initialize Magento and hide sensitive config data below site root
        $name='frontend';
        $options = array('etc_dir' => realpath('../magento-etc'));
        Mage::app('default','store', $options);
        Mage::getSingleton("core/session", array("name" => $name));

        $websiteId = Mage::app()->getWebsite()->getId();
        echo "websiteid: $websiteId<br>";
        $store = Mage::app()->getStore();

        $customer = Mage::getModel("customer/customer");
        $customer->website_id = $websiteId;
        $customer->setStore($store);
        echo 'customerwebsiteId: ' . $customer->website_id . '<br>';

        $session = Mage::getSingleton('customer/session');
        $magento_message = 'Welcome ';
        // Generate a personalize greeting
        if($session->isLoggedIn()){
            $magento_message .= $session->getCustomer()->getData('firstname').' ';
            $magento_message .= $session->getCustomer()->getData('lastname').'!';
        }else{
            $magento_message .= 'Guest!';
        }

        echo $magento_message;

    }
}

CodeIgniter is doing something that I have not been able to track yet. The websiteId is returned correctly, but isLoggedIn returns false.

Anyone have any ideas? THANKS!!

#0

I use both but ive never tried to mash them like that. I foresee quite a few problems.

How are you patching into magento?

You might need two db connections running :

$db['magento']
$db['default'] // codeigniter default

Sessions could become a real problem here also aswell as config data.

Consider sticking with magento for now, then maybe patch into your blog/website via a RESTFul service.

#1

Both code examples above work fine. The problem I had was calling session_start() near the top of the CodeIgniter index.php file. Once that was removed, it all started working.

For posterity, here is a Magento 10 Library for CodeIgniter 2.1:

application/libraries/magento.php

<?php if ( ! defined('BASEPATH')) exit("No direct script access allowed");

Class Magento {

  function __construct($params)
  {
    global $site_root;
    $name = $params['name'];
    // Include Magento application
    require_once ($site_root . '/store/app/Mage.php');
    umask(0);
    // Initialize Magento and hide sensitive config data below site root
    // Uncomment next line if you have moved app/etc
    // $options = array('etc_dir' => realpath('../magento-etc'));
    Mage::app('default','store', $options=null);
    return Mage::getSingleton("core/session", array("name" => $name));
  }
}
// end of magento.php

Usage example app/model/test_mage.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Test_mage extends CI_Controller {

  function __construct()
  {
    parent::__construct();
    $params = array('name' => 'frontend'); // frontend or adminhtml
    $this->load->library('magento', $params);
  }

  public function index()
  {
    $session = Mage::getSingleton('customer/session');
    $magento_message = 'Welcome ';
    // Generate a personalize greeting
    if ($session->isLoggedIn())
    {
      $magento_message .= $session->getCustomer()->getData('firstname').' ';
      $magento_message .= $session->getCustomer()->getData('lastname').'!';
    }
    else
      $magento_message .= 'Guest!';

    echo $magento_message . '<br>';
  }
}
// end of test_mage.php

推荐文章

为什么不能将向前声明用于std :: vector?

为什么不能将向前声明用于std :: vector?

推荐文章

如何在Linq to SQL中实现缓存?

如何在Linq to SQL中实现缓存?

推荐文章

如何对WCF服务进行单元测试?

如何对WCF服务进行单元测试?

推荐文章

如何说服我的同事不要将数据集用于企业开发(.NET 2.0+)

如何说服我的同事不要将数据集用于企业开发(.NET 2.0+)

推荐文章

将可可前端写入Erlang应用程序的好方法是什么?

将可可前端写入Erlang应用程序的好方法是什么?

推荐文章

在USB密钥上安装MSDN

在USB密钥上安装MSDN

推荐文章

PHP中任意大整数的算术运算

PHP中任意大整数的算术运算

推荐文章

Linux轻量级发行版和X Windows开发版

Linux轻量级发行版和X Windows开发版

推荐文章

如何使用Visual Studio Express 2005制作完全静态链接的.exe?

如何使用Visual Studio Express 2005制作完全静态链接的.exe?

推荐文章

将Perfmon分析解释为特定于应用程序的观察值/数据的最佳方法是什么?

将Perfmon分析解释为特定于应用程序的观察值/数据的最佳方法是什么?

推荐文章

回到基础。如何重新使用C ++?

回到基础。如何重新使用C ++?

推荐文章

SQL差异的原因

SQL差异的原因

推荐文章

等同于StAX for C

等同于StAX for C

推荐文章

iPhone App减去App​​ Store?

iPhone App减去App​​ Store?

推荐文章

如何从网页确定主机的已安装ASP.NET版本

如何从网页确定主机的已安装ASP.NET版本

推荐文章

这个最小生成树算法正确吗?

这个最小生成树算法正确吗?