Hi I am facing a big problem right now in developing my application. Actually there is a function developed already in one of our previous application class file. Now we want to use that function in our new application. How can we call that function in our new controller using codeIgniter.
Note: our old application is in core PHP.
old file report.class.php....in this there is a function name get_report()
New controller ro_manager.php.......in this there is a function order_details()...
I want to call get_report() function in order_details() function......i need help in this any idea.......
Codeigniter is just a framework. You can still use native php without any problem(Paths may need a little bit modification).
Just make it as a library and load it via $this->load->library('libraryName');
or else, you can include it in naive way. create an instantiation in the controller and use it just like any other OOP project.
Creating codeigniter libraries The documentation includes all the procedure you will need (with examples).
As far I can see, you’re wanting to either create a library with this old function in, and load it in your CodeIgniter controllers; or just add the function in your controller as a private method if it’s to only ever be used in that controller.
Related
I am trying to use guzzle 6.x on a CakePHP 2.x application.
What I need to do is to initialize Guzzle Client to some of my controllers but on the controllers that it will be loaded I need it to be loaded with the same configuration.
Basically what I don't know is which is the best approach to implement it. I was thinking about the following:
Shall I create a function in AppController that will create and return a Guzzle object and then save it to a protected property inside AppController? Maybe a function like setUpGuzzle() and call this function on the Controllers I need to load Guzzle Client.
Shall I create a component and then load Guzzle Client to a public property of this component. Then I could use it like this $this->HttpClient->client->post()
Shall I create a component and write one function for each of Guzzle public function? So I will have something like this $this->HttpClient->post().
Thing is I don't like any of the above and I was hopping maybe there could be another way to do this. For example create a components which loads the Guzzle Client in a controller or loads the Guzzle Client inside the component collection.
Do you really need Guzzle? I agree that the old Cake2 HTTP socket is old fashioned but is there something it can't do that requires you to add yet another lib?
Use a trait, as long as you're not stuck to an ancient php version this is a clean solution. Here is some pseudo-code that will give you the high level idea:
trait HttpSocket {
protected $_httpSocket = null;
protected $_httpSocketConfig = [
// Default config goes here
];
public function getHttpSocket() {
if (empty($this->_httpSocket)) {
// Not sure how the constructur works, so it's just an example
$this->_httpSocket = new Guzzle($this->_httpSocketConfig);
}
return $this->_httpSocket;
}
}
If you ever need to change the config or the whole socket implement ion you just have to change it in a single place without the overhead of a component. Also this can be use in any class, not just controllers. What you're looking for is more or less a simple factory like method, no need for a whole controller.
If you can't use a trait then you'll have to use a component or just put the above code not inside a trait nor a component but directly inside your AppController, but you won't be able to use it outside the scope of the controllers that inherit that controller then.
I am new to cakephp and I managed to work my way out doing some great stuff with this powerful framewoek. Seeking some organization in my code, I am looking for a way to create some kind of side library to call it whenever it.
I am working on the mailing layer of my application using the built in mailing utility, so I want to know how to create a library where i can store my functions.
Thank you.
you can create your own library file in
/app/Lib/Library_Class_name.php
class Library_Class_name {
public function function_name() {
}
}
after this you should need to use load this Library file to your application use below code
App::uses('Library_Class_name', 'Lib');
One this, If you want to load this file for entire application you need to use above App::uses code to your Config/bootstrap.php file otherwise you can use this to needed files or functions.
Hello and thanks for reading.
I'll get straight to the point: I have a website project that I've been building using CodeIgniter 1.7.3, which I have thoroughly enjoyed using, however I have been contemplating upgrading to CI 2.0+.
I tried a straight copy, just moved my folders for controllers, models and views over to a CI 2.0 framework, but I got a 500 server error when I tried to view my pages.
After doing some investigation I discovered that all of your controllers must now use "CI_Controller" as their parent class. Also I noticed that if you want to include a constructor in your controller class that it must use the syntax "function __construct()" as its name and of the parent class. It seems that CI 2.0+ no longer supports using a constructor that has the same name as the class name, e.g. "class Blogs extends CI_controller{ function Blogs(){parent::__construct();}}" is no longer supported?
I've been reading the CI Change Log, but all I see are bug fixes, and new features, nothing about compatibility issues with older versions of CI?
Does anyone else know of any other secret little pitfalls?
Thanks,
H
CI 2.x removed all compatibility with PHP4 and also updated a number of standards to be compliant with PHP 5.3 going forward. One of these is the constructor issue you have encountered. As of PHP 5.3, the function ClassName() is no longer the constructor for a class, it is simply another function. You must explicitly declare a __construct function to perform any tasks that need to be done when a new instance of the class is created. Given this, you should see it no longer makes sense to call parent::ClassName() in your child constructor as that function would no longer be the parent's constructor.
Another pitfall I recently had to work out is how the $_GET array is now handled. In the 1.x versions, you could use query strings to pass back extra information and still use URI segments to route to controllers and functions. This is especially useful for AJAX calls where you may not always know all the parameters being sent to and from the server in a particular request. In the 2.x versions, the config.php file contains a new option, $config['allow_get_array']. This must be set to TRUE if you want to use query strings otherwise the input class clears out the $_GET array as part of CI's initialization routine on each request.
Something which isn't a pitfall but you may find useful is the new options in config/autoload.php that allows you to add new application directories to your project. If you work on a number of different projects with CI and want to keep any useful libraries you write in a single location, you can now add that location to $autoload['packages']. CI expects any path in this array to contain the sub-directories "controllers", "models", "libraries" and "helpers". It won't complain if you don't have those directories but you will at least need them for anything you intend to load, i.e. libraries would live in /libraries as with the main application folder.
Have you read the official guide for upgrading from 1.7.x to 2.x ?
so in short
Update Models and Controllers to
extend CI_Model and CI_Controller
Update Parent Constructor calls
class Wow extends CI_Controller {
function __construct()
{
parent::__construct();
//your stuff
}
function index()
{
// just for example
}
}
I'm building a library for our CodeIgniter app, but it requires many classes (currently I'm at 12).
Is there a best practice for packaging these many clients into one library. So I can just make one call to load it. i.e:
$this->load->library('soaplibrary');
Thanks!
As Summer points out, they have handled this situation somewhat elegantly in CI 2.0 with the concept of Drivers.
With a Driver, you actually create a subdirectory within your 'libraries' directory that contains your 'super' class, and another directory for 'child' classes. Better visual representation of the structure...
This was taken from Here.
and once you have constructed your library, here is the documentation on how to use it.
In CI 2.0, there are drivers to handle this situation. Good luck!
In CodeIgniter 3.1.9 when you load a library file, all classes in this file are included into code.
Let's say in soaplibrary.php you have
class SoapLibrary {
public function someMethod(...
class Test {
public function anotherMethod(...
In your controller you can do:
$this->load->library('soaplibrary');
//now on you can do
$this->soaplibrary->someMethod();
//but also
$test = new Test();
$test->anotherMethod();
CodeIgniter attempts to call the constructor of class SoapLibrary, hence a class with that name must be in there.
I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John
What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.
If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .