Accessing CodeIgniter library through ExpressionEngine - php

I'm not sure if this is possible, but various posts in the ExpressionEngine forums and in the documentation suggest that I should be able to let ExpressionEngine know what's going on in the CodeIgniter foundation that it's built on.
So, in light of that, I've got a CodeIgniter app, and an ExpressionEngine site. The /system directory is structured like this:
/system
/system/codeigniter
/system/codeigniter/application (this is my existing CodeIgniter application)
/system/codeigniter/system (this is the same CI folder that EE runs from)
/system/expressionengine
... (this is the normal EE - templates, add-ons, etc.
I'm fairly new to ExpressionEngine, but my understanding was that I could use this setup to built an EE module that would integrate well with CodeIgniter.
Currently, I have a module setup, and a method in it that does this, just as a starting point:
function __construct()
{
$this->EE =& get_instance();
$this->CI =& get_instance();
$this->auth =& $this->CI->load->library('mylibrary');
}
But then it doesn't recognize the library when I load the module (this is all happening inside the EE control panel, at this point, just so I can make a connection between the two). Says it doesn't exist. Is there anything I can do to make this connection, or am I going the wrong direction entirely?
Thanks,
Jonathan

Okay, thanks to http://expressionengine.com/forums/viewthread/208140/ I have an answer I think I can build upon.
Here's how it works, for posterity's sake:
EE and your module don’t know at all about your system/codeigniter/application folder. Try this, it > might work:
$this->EE =& get_instance();
$this->EE->load->add_package_path(BASEPATH.'../application/');
$this->EE->load->library('mylibrary');
$this->EE->mylibrary->do_stuff();
From there I was able to load my library, but it had issues loading its language file. The same thread gave an answer to that, as well. This code goes into the CI library file, and EE translates it as it loads.
$this->ci->lang->load('mylangfile', '', FALSE, TRUE, BASEPATH.'../application/');

In the past I've written simple wrapper add-ons for EE to access the CodeIgniter helpers, etc. Here's an example of a simple wrapper add-on.
Another option, it should be possible to this with PHP, which means you'll need to enable PHP in your template. In short, you load the helper/library/etc and then call the method you want. More info on how to do that in the EE docs.

You should simply put your library in /third_party/my_addon/libraries/ folder, then load it like so:
function __construct()
{
$this->EE =& get_instance();
$this->auth = $this->EE->load->library('../third_party/mhy_addon/libraries/mylibrary');
}

Related

Load Database in core file codeigniter

I am creating a core file in codeigniter for user and want to run some database queries there as well. core file is for users so when ever i open website users data hould be available automatically and no need to create a library and call it again and again.
I have tried autoloader and Codeigniter.php file but its not working.Do anyone have idea how to achive this. so that i can use 'user' function easily like. $this->user->login() , $this->user->logout() ... same as some builtin functions pre-loaded like $this->input->get() or $this->load->SOMETHING()... etc etc.
You can try hooks for db in core file. enable hooks on config.php
$config['enable_hooks'] = TRUE;
and use this for more see this https://codeigniter.com/userguide3/general/hooks.html
$CI =& get_instance();
$this->CI->db->select('*')

Loading Wordpress functions inside Zend Framework

Hio,
So I have a website which uses the Zend Framework as MVC (www.site.com), and a separate blog set up in wordpress (www.site.com/blog) on the same server, but I want to be able to use Wordpress functions on various pages to pull posts from wordpress.
Currently, the webservers DocumentRoot is /httpdocs/public. public contains a symlink to /httpdocs/blog (I have Options FollowSymlinks on)
All requests are routed through htaccess rules which either redirects it to httpdocs/public/index.php (which then loads Zend stuff) or a regular file/other area not inside the Zend application.
The example code in wordpress is to use the following:
define('WP_USE_THEMES', false);
require(APPLICATION_PATH .'/../blog/wp-blog-header.php');
But... this just tries to redirect me from site.com/page to site.com/page/wp-admin/install.php (which doesn't exist, and so I get a 'this page is redirecting in a way that will never complete' error from firefox, even though site.com/blog is setup and works perfectly fine. For some reason it doesn't seem to recognise that it is (probably because of path issues...)
Does anyone know how I could fix this??
Note: at the moment I just query the wordpress database, but this doesn't work properly because its not formatting the post content properly.
This is a known issue (Tested with WP 3.2.1 at the time of writing):
http://wordpress.org/support/topic/calling-wp-blog-headerphp-from-inside-a-php-function
The solution:
Avoid including the wordpress core inside any class method or function. If it still doesn't work, check the contents of debug_backtrace() to see if you really are on the global scope.
The explanation:
Wordpress sets some variables (direct assignment, does not use $GLOBALS) during initialization. If it's not initialized on the global scope, these variables belong to the current function or method scope. The result is that these variables cannot be accesed by wordpress's core functions, as they rely on the global keyword.
Benno,
I think I found a work around for this. azkotoki was right, it seems to be an issue of scope when including the wp-blog-header.php or wp-load.php files.
The headers have to be included at a global scope or else Wordpress gets confused and doesn't think its installed and redirects to wp-admin/install.php.
I made my require_once call right along with my Zend application require_once call in index.php of my Zend application's /public/ directory. Unfortunately, this means the Wordpress header will be included even when unnecessary, but at least its working.
...From /public/index.php...
/** Zend_Application */
require_once 'Zend/Application.php';
/** Wordpress Application */
require_once 'wordpress/wp-load.php';
Hope this helps, as it was driving me crazy!

Developing/using a custom Resource Plugin in Zend Framework

We have used Zend_Log, which is configured in application.ini differently for different circumstances. We initialize it/get it in the bootstrap and store it in the registry:
$r = $this->getPluginResource('log');
$logger = $r->getLog();
But we've subclassed Zend_Log (say, Our_Log) to add customized features, and want to get it the same way. So then we have to make a new Resource Plugin. That seems quite easy - just copy Application/Resource/Log.php, rename the file to Ourlog.php, rename the class to class Zend_Application_Resource_Ourlog. For now, let's not worry about "Our_Log", the class -- just use the new Resource Plugin to get a Zend_Log, to reduce the variables.
So then, our new code in the bootstrap is:
$r = $this->getPluginResource('ourlog');
$logger = $r->getLog();
but of course this doesn't work, error applying method to non-object "r". According to the documentation,
"As long as you register the prefix path for this resource plugin, you
can then use it in your application."
but how do you register a prefix path? That would have been helpful. But that shouldn't matter, I used the same prefix path as the default, and I know the file is being read because I "require" it.
Anyway, any guidance on what simple step I'm missing would be greatly appreciated.
Thanks for the pointers -- so close, so close (I think). I thought I was getting it...
Okay, so I renamed the class Xyz_Resource_Xyzlog, I put it in library/Xyz/Resource/Xyzlog.php
Then, because I don't love ini files, in the bootstrap I put:
$loader=$this->getPluginLoader();
$loader->addPrefixPath('Xyz_Resource','Xyz/Resource/');
$r = $this->getPluginResource('xyzlog');
if (!is_object($r)) die ('Not an object!!');
Sudden Death. So, okay, do the ini:
pluginPaths.Xyz_Resource='Xyz/Resource/'
The same. No help. I believed that the basepaths of the plugin paths would include the PHP "include" paths. Am I mistaken in that? Any other ideas? I'll happily write up what finally works for me to help some other poor soul in this circumstance. Something to do with Name Spaces, maybe?
Plugin classes are resolved using the plugin loader, which works slightly differently to the autoloader; so just requiring the class in doesn't help you here. Instead, add this to your application.ini:
pluginPaths.Application_Resource = "Application/Resource"
you should then be able to use the class as normal. Since your path above will be checked before the default Zend one, you can also name your class 'Log' and still extend the Logger resource to override the standard functionality.

CakePHP Facebook integration tutorial "Fatal error: Call to a member function share() on a non-object" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call to a member function on a non-object
I'm doing this tutorial here:
http://tv.cakephp.org/video/webtechnick/2011/01/12/nick_baker_--_facebook_integration_with_cakephp
I baked a new project with cake bake facebook_app and set the configuration database file to the correct settings and the default cakePHP screen showed that tmp directory was writable, DB setup was good, etc.
I downloaded the CakePHP plugin by WebTechNick here: https://github.com/webtechnick/CakePHP-Facebook-Plugin, and filled out app information (app_secret, app_id, etc), adding it to facebook_app/config/facebook.php
Changed facebook_app/app_controller.php:
class AppController extends Controller {
var $name = 'Facebook';
var $helpers = array('Session', 'Facebook.Facebook');
}
Then just exactly as in the tutorial `facebook_app/views/pages/home.ctp':
<h1>Facebook_App</h1>
<?php $this->Facebook->share(); ?>
returning the error message:
Undefined property: View::$Facebook
I realize that means PHP didn't recognize Facebook as an object. But I installed the plugin!
Also, it seems not MVCish to have something like $this->Facebook->share(); in a view (home.ctp). However, this is exactly how WebTechNick does it in his tutorial (I followed it exactly 3x) and it does not work for me. I'm a complete noob at cakePHP (although I've read the entire documentation) and I'm just trying to learn and understand through examples.
:) To be fair, it's PHP - you didn't install anything. Or if you prefer, "install" != "invoke." PHP is really amazingly easy to debug. I mean, it tells you exactly what's wrong:
Like turning to a channel that's not on the air, the error your getting means that the object you're calling doesn't actually exist, at least not in the scope you're trying to invoke it.
Is that your IDE? Is it set up for your Cake app? Are you sure the instructions were to set your AppController's $name to 'Facebook' instead of $name = Facebook_App in your AppController? It looks like you either replaced your actual app's AppController with the plugin files instead of putting them in the proper directory, or the plugin is not deferring / calling / extending / returning to the application the way it's supposed to. Knee jerk -> typo, naming conflict, path problem, permissions.
Cake's not even rendering. I can tell because your screenshot would show that error with the styled Cake errors. That tells you it's erroring before AppController class makes it to View class.
Create an instance of the Facebook object statically in the view and see what happens. Then, what does
function beforeFilter() {
parent::__construct() ?
}
get you? Anything? What about debug(), var_dump, the object functions will also shed light on what's happening. So will your logfiles.
Btw, if you don't use them already: Firefox + FirePHP + Xdebug = made of win.
I was having this problem and I found that the plugin I was using was for CakePHP 1.3 and I was using Cake 2.0. I found the BETA branch for the upgraded Cake 2.0 and it worked perfect.
Here is the Cake 2.0 BETA Branch

Is this Codeigniter code correct?

I've just started looking at PHP Frameworks after spending loads of wasted time doing everything from scratch. I thought I would give Codeigniter a go after a friend recommended it to me.
I worked through the first tutorial and there were no issues but I'm getting stuck with the second one.
At first my code was identical to the tutorial:
<?php
class Blog extends CI_Controller{
function Blog()
{
parent::CI_Controller();
$this->load->scaffolding('entries');
}
public function index(){
$data['title'] = "My Blog Title";
$data['heading'] = "An intresting title";
$data['todo'] = array('create media player','design site','finish project');
$this->load->view('blog_view',$data);
}
}
?>
But I got an internal server error. After looking at the user guide it shows a different way for using the constructor.
I changed the code to the document spec.
<?php
class Blog extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->scaffolding('entries');
}
public function index(){
$data['title'] = "My Blog Title";
$data['heading'] = "An intresting title";
$data['todo'] = array('create media player','design site','finish project');
$this->load->view('blog_view',$data);
}
}
?>
But I still get an internal error. I don't know if my code is wrong or I misconfigured something else. Some advice would be good, thanks.
Edit:
To clarify - I only get an internal error when I add the constructor to the class.
This will most likely because you're trying to load scaffolding in the latest version of CodeIgniter.
Scaffolding has been depreciated since version 1.6. It was removed in 2.0, and the latest version is now 2.0.2
In terms of which of your provided snippets to use, the latter form of declaring a constructor (__construct) is preferred over using the Class name (Blog). This method is also consistent with the Core Classes of CodeIgniter since 2.0.
As an aside, I've got no idea why CodeIgniter are including such out-of-date samples in their tutorials. EllisLab's seem to have lost total interest in the CI project recently, which is a shame :(
Internal error means there's mis-server configuration.
Usually .htaccess file or apache settings. Try this:
Enable rewrite module in apache.
Make sure .htaccess file is configured correctly. It should look like this: https://gist.github.com/ce2914e79193896590bc
This also removes index.php, which you should look into.

Categories