I have to adapt Codeigniter module to Wordpress. It fetches data with SOAP protocol. It needs to use Codeigniter's csrf_protection.
How to include Codeigniter MY_Controller class in Wordpress?
class Balance extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->config->set_item('csrf_protection', TRUE);
require_once('addons/libraries/nusoap/nusoap.php');
}
...
You need to copy the methods in the nusoap file and try to make like other libraries setup in codeigniter library folder and then :
$this->load->library('nusoap/nusoap')
Nusoap is the class name which is in upper case letter start with and call in controller like above.
Or else you can find / download separate controller codeingiter library file from github
check it and mark accepted if it works!.
Thanks
Related
Hi I have a file called payment.php it contains some functions related to payments and some multiple classess. So I want to include that file in my symfony2 Controller to access its all methods and classless. I am trying it as follows:
//File location is in src/AppBundle/Controller/payment.php
namespace AppBundle\Controller;
require_once __DIR__.'/./payment.php';
//My controller
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
But I am not able to access the file and its methods.
I am using this approach because keeping it in /vendor directory it also not able to access because this file contains multiple classless in same files.
Please advice me how can I access this file in my controller.
thanks in advance
If paymant.php have classes you need to make instance of that class to call method from it, that's like basic OOP stuff.
class ApiServicesController extends Controller
{
$this->payment(array('txnId'=>1112548));
}
First of all, where is the method in this controller where you want to call your method? Then, why you are calling payment on $this if it comes from diffrent class. It should be something like this
class ApiServicesController extends Controller
{
public function indexAction()
{
$somePaymentClass = new SomePaymantClass(); //whatever class you want from payment.php
$somePaymentClass->payment(array('txnId'=>1112548));
}
}
But iI strongly recommend to use it as a service and put it in some autoloader namespace.
You have to make a Payment class as a service and then you can use all functions of Payment class in controller. Please refer this document.
http://symfony.com/doc/current/service_container.html
I have trying to override the email library by doing the following steps:
Create file core/MY_Email.php
In file add code below
<?php
class MY_Email extends CI_Email {
public function __construct()
{
parent::__construct();
}
?>
Load library is normal
$this->load->library('email');
I put a die in the top of my class and it appears to not even be included and just uses the regular system email class. I need to extend it to override _smtp_connect(). I have done this with other classes.
I am using CI 3.0.4. I have tried to trace through the loader class; but cannot figure it out.
Put MY_Email.php in application/libraries
You need to put the file in application/libraries NOT application/core
$this->load->library('email');
Create a new file called MY_Email.php and put it in application/libraries/
In my CI 2.2 project I want to make my parent controll with app's common functionality for use in all app and for this I create file :
application/libraries/N_Controller.php :
<?php
class N_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
But on first attempt to use it in file
application/controllers/admin/admin.php
<?php
class Admin extends N_Controller
{
public function __construct()
{
parent::__construct();
I got error:
PHP Fatal error: Class 'N_Controller' not found in /controllers/admin/admin.php on line 3, referer: http://local-ci22.com/admin/hostel/edit/15
I tried to add in application/config/autoload.php file :
$autoload['libraries'] = array( 'AppSmarty', 'AppUtils', 'N_Controller');
But it did not help. Which is the correct way ?
if you want to extend the functionality of a system class. you need to follow this recomendations.
place your extended class in /application/core, be sure that you name it exactly like the name and casing of the class created.
your class should extend from CI_Model or CI_Controller, depending on your needs.
wherever you implement your new class, be sure that you honor the same name casing from your extended class.
you should have configured the $config['subclass_prefix'] on /application/config/config.php. let's say in your case with the value 'N_'
what i can see from your code, you are not extending from CI_Controller and your path seems wrong.
Informative note: the /application/library is used to place classes and libraries from 3rd parties that won't fit into CI schemas.
I'm trying to call a library from model in codeigniter but doesn't want to fire and I don't really know how to debug it, I have been trying to use codeigniters default logging method but doesn't show anything. My code looks as it follows.
class Model_products extends CI_Model{
function __construct(){
parent::__construct();
$this->load->library('ApiClient');
log_message('error', $this->load->library('ApiClient'));
}
}
/application/libraries/ApiClient.php
libary final class ApiClient
{}
You cannot name a library ApiClient - class names should follow the CI naming convention (which is identical to ucfirst()) and when loading anything in CI you need to load it with lowercase naming.
Naming is the key.
Your file should be named Apiclient.php (libraries has the first letter in uppercase in the filenames, models, views and controller are all lowercase filenaming).
Your class definition should reflect this also:
class Apiclient
{
...
}
And when using the CI instance to load the library, you need to load it with lowercase naming:
$this->load->library('apiclient');
You miss a semicolon at the end of your line $this->load->library('ApiClient'). Could this be it?
class Api extends CI_Controller {
public function index()
{
show_error("You are not authorized to access this page", 401);
}
I have an api class class Api extends CI_Controller and an another
class myproject extends , now if want to use the functions of the api class in myproject class. how can i do . do i have to create an object of api class or just extend the myproject class with parent::api class . please help me as i m not good at oops.
details - i have made a class "class myproject extends ci_controller" which has different functions for user registration and login application. it all works fine using a single controller. But now want to use an api file.which has functions for login. how can i call those functions in api file from "class myproject"
You can create an helper, like the form one and use your method.
Then you can load it:
$this->load->helper('api');
First of all this looks more like a software design problem rather then a real code issue.
CodeIgniter is based on the MVC concept, and the Controller is only meant to build up the page.
You should probably make a library for all the API functionality, and have the controller calling that library and converting the data to JSON or whatever you want it to be converted to.
For myself i apply a simple rule:
Never write/call a Controller method that doesn't generate any form of output.
I recommend you to stick to the CodeIgniter way of coding, and not try to avoid them.
You should just need to extend the API class, as in
class Myproject extends Api { ... }
but there a few other issues, such as include_once("Api.php"), for example, in the Myproject.php file so that the subclass can "see" the superclass declaration. This thread on the codeigniter forums discusses these issues (sorry for the google webcache version but the forums are down for maintenance at the moment).
Firstly, you can't call a function of a controller in a controller unless and until you are inheriting it.
It's better that you create API class as library, instead of a controller.
If you have some restriction to have it as a controller only then use these APIs through curl calls.
But the best way is to have them in helper, if you are accessing them from your own server only.