• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to write a Library for the new Xajax

#1
Hello All,

I'm trying to write a library for the newly resurrected Xajax project (from Thierry Feuzeu)...

and I'm coming up short due to my inexperience in writing CodeIgniter Libraries and with PHP namespacing.

Could anyone offer some thoughts on how to make this work? (see's Thierry's example code: https://github.com/lagdo/xajax-examples/.../hello.php)

Here's what I've got so far, and it definitely instantiates an xajax object, of which I can access at least the getVersion() method from inside a CodeIgniter controller method, but I can't figure out how to write the Library to access other basics (see errors in __construct() ):
( output: http://triv.ath.cx/ci3/ )

So, I'm at a loss as to what to do next... any advice?

CodeIgniter 3 Library:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require (
APPPATH '../vendor/autoload.php');

use 
Xajax\Xajax;
use 
Xajax\Response\Response;
use 
Xajax\Request\Factory as xr;

class 
CI_Xajax {

 
 public $xajax '';

 
   public function __construct()
 
   {

 
   $this->xajax Xajax::getInstance();
//        $this->_ci   =& get_instance();
 
       
    $this
->xajax->setOption('core.debug.on'true);
 
   $this->xajax->setOption('core.prefix.function''xajax_');
 
       
    
}


CodeIgniter 3 Welcome Controller:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

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

 
       $this->load->library(array('CI_Xajax''session'));

//    $this->ci_xajax->xajax->register(Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Class 'Xajax' not found in /var/www/html/ci3/application/controllers/Welcome.php on line 12

//    $this->ci_xajax->xajax->register(CI_Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Undefined class constant 'USER_FUNCTION' in /var/www/html/ci3/application/controllers/Welcome.php on line 15

 
   }

 
   public function index()
 
   {
 
   $viewData '';
 
   $viewData['XjxVersion'] = $this->ci_xajax->xajax->getVersion();

 
       $this->load->view('welcome_message',$viewData);
 
       
    
}
 
   
    public 
function hello() {
 
       $this->load->view('welcome_message',$viewData);
 
   }


(Using CodeIgniter since 1.x)
Reply

#2
Hi, I used it with CI 2.X the link below may help you figure it out:

Xajax CodeIgniter

I will play around with it later I used it alot with CI 2.X and see if I can get it to work.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(04-26-2016, 03:16 AM)InsiteFX Wrote: Hi, I used it with CI 2.X the link below may elp you figure it out:

Xajax CodeIgniter

I will play around with it later I used it alot with CI 2.X and see if I can get it to work.

Thanks for your reply InsiteFX!

I've already got the older version of Xajax working with CI3, it's the new one that I'm working on.
The new author of Xajax, Thierry Feuzeu, is suggesting that we implement it not as a library, but as a Controller that we extend.

I know the CI tradition is to implement such things as libraries (that's how I implemented old xajax+CI3) ... how do you feel about Thierry's implement-in-base-controller-instead-of-library suggestion?

Thanks!

(Using CodeIgniter since 1.x)
Reply

#4
If he says to implement it as a Controller then I would add it to a MY_Controller and extend all your other Controllers from it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(04-26-2016, 01:00 PM)InsiteFX Wrote: If he says to implement it as a Controller then I would add it to a MY_Controller and extend all your other Controllers from it.

So far, that's not working... see my results here.

(Using CodeIgniter since 1.x)
Reply

#6
Try loading the library using CI's loader.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
Hi, jasonzig

I've been using xajax last years by a very easy way using as a library. I think library is more convenient and clear.
I did it according to my own thoughts and codes. But the links below show something similar to my code:

http://www.jerictan.com/live/how-install...er-2-0-988

Here, forget this fix and keep the focus on how to make Xajax work:
http://www.gen.newrandom.com/2011/07/21/...-csrf-fix/

If need any help, do not hesitate to ask.
Reply

#8
PHP Code:
//    $this->ci_xajax->xajax->register(Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Class 'Xajax' not found in /var/www/html/ci3/application/controllers/Welcome.php on line 12 

In this case, Xajax is defined in the context of your library, but not in the context of your controller. The error is specifically the use of Xajax::USER_FUNCTION as the first argument to $this->ci_ajax_xajax->register().

PHP Code:
//    $this->ci_xajax->xajax->register(CI_Xajax::USER_FUNCTION, 'hello');
//    Fatal error: Undefined class constant 'USER_FUNCTION' in /var/www/html/ci3/application/controllers/Welcome.php on line 15 

Here you've bypassed the previous error by referring to the class name of your library (though you would normally refer to this via $this->ci_xajax), but your library doesn't define the USER_FUNCTION constant. This would be defined by doing something like this:

PHP Code:
class CI_Xajax {
    const 
USER_FUNCTION Xajax::USER_FUNCTION

Then your controller's constructor could do something like this:

PHP Code:
$this->ci_xajax->xajax->register($this->ci_xajax::USER_FUNCTION'hello'); 

Some of this may be PHP-version-dependant, and I haven't tried it (and the syntax I've used looks questionable in the second example, you may need to use $this->ci_xajax->USER_FUNCTION).
Reply

#9
I've never understood the appeal of doing this. A server is a data store the client is a presentation layer. Trying to merge client concepts with a server creates an unmanageable mess for no perceivable gain.

How are you going to manage exceptions?

What if the DOM element you target server side has been removed from the client?

Why would you ever want to mix business logic between two systems when it only applies to one?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.