How to create my own library on CakePHP - php

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.

Related

Use keyword replacement from core php to codeigniter

I am trying to use core php library in codeigniter. After inculding files in php code, It contain code as:
use Mylib\Configuration\GenericConfiguration;
use Mylib\Operations\Search;
I am getting error on using 'use' keyword and I am not able to find how to use this files in codeigniter code. So, Please help me to find solution for this problem.
Thanks in advance.
You can use CodeIgniter libraries that might help you to avoid use keyword
In CodeIgniter, libraries folder is available in
[CI_ROOT]/application/libraries/
For this simply place your all Mylib\Configuration\GenericConfiguration, Mylib\Operations\Search class files into the libraries folder
Then, you can mention these libraries in autoload.php ([CI_ROOT]/application/config/autoload.php) as
$autoload['libraries'] = array('database','session','[Your_Library_Class Name_In_Small_Letters]');
or, if you want to use in a specific controller or in a specific method, use like
$this->load->library('[Your_Library_Class_Name_In_Small_Letters]');
after doing this, you can call your library functions with its instance like
$this->[Your_Library_Class Name_In_Small_Letters]->libraryFunctionName();
See Codeigniter documentation for brief explanation.

Creating Composer Package

I'm trying to create a composer package & i understand the basic workflow like of creating composer.json, auto loading and creating classes under src directory.
There is one small programming misunderstanding i have is that almost all the other packages i'm reading has interfaces and a class implementing them. I don't understand need of interfaces in this context and why we need them. I have never used interface or i'm not sure if i understand its general use case. It would be nice if someone can help me understand it.
Beside the other question i had in context to composer is how do i test / run a composer project whilst i create it?
Beside this projects that i'm referring has a command directory inside src i don't understand significance of this or its use case too. I guess it has something to do with symfony php console command.
Also there is a bin directory at source, now how is that useful.
Sorry if i'm being naive here but i'm just trying to understand which components fall where and why is it like that. I couldn't find a composer tutorial online past creating composer.json
You are asking a lot of questions at once, but I'll try to at least address interfaces, since I believe that's the most important one.
Interfaces are mostly used with Dependency Injection. They define methods without actually caring how the methods are actually implemented. A class may depend on an interface instead of an actual (concrete) class, which allows an easy way to swap components. Below is an example of how one might use interfaces.
interface PostsInterface {
public function getPosts();
}
class JsonPostFetcher implements PostsInterface {
public function getPosts() {
// Load posts from JSON files here
}
}
class MySqlPostFetcher implement PostsInterface {
public function getPosts {
// Load posts from a MySQL database
}
}
class Blog {
public function __construct(PostsInterface $fetcher) {
// Load posts from either JSON or a database
// depending on which fetcher is provided
$posts = $fetcher->getPosts();
}
}
Using this method anyone can now write their own code to provide posts from an external API ApiPostFetcher, SQLite Database SqlitePostFetcher, Serialized PHP files SerializedPostFetcher etc. One could even write a DymmyPostFetcher that simply returns a pretermined array of posts that could be used for testing purposes. You can then use any implementation of PostsInterface in your blog like in the following example.
$fetcher = new JsonPostFetcher(); // You can provide different fetchers here.
$blog = new Blog($fetcher);
If you're unfamiliar with dependency injection, I highly recommend learning it, since it will be especially useful in writing modular code.

How to call an external api from codeIgniter?

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.

codeigniter - best practice library with many classes

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.

Loading custom classes in CodeIgniter?

Just starting to use CodeIgniter, and I'd like to import some of my old classes for use in a new project. However, I don't want to modify them too much to fit into the CI way of doing things, and I'd like to be able to continue to use NetBeans' autocomplete functionality, which doesn't work too well with CI.
So, what is the best way to load custom classes & class files into CodeIgniter without having to use the library/model loading mechanisms?
I apologise if this is something I should be able to find quickly, but I can't seem to find what I'm after. Everything I see is just telling me how to go through CI.
To do it codeigniter way, place your custom classes in libraries folder of codeigniter. And then use it by adding that class as library in your controller like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
public function some_function()
{
}
}
/* End of file Someclass.php */
using in controller:
$this->load->library('someclass');
checkout complete article at http://www.codeigniter.com/user_guide/general/creating_libraries.html
Libraries are easy to write but they have a few restrictions. Constructors can only take an array as a parameter and it's assumed that only one class will exist per file.
You can include any of your own classes to work with them however you want, as this is only PHP ofc :)
include APPPATH . 'classes/foo.php';
$foo = new Foo;
Or set up an __autoload() function in your config.php (best place for it to be) and you can have access to your classes without having to include them.
I'd say you at least write a wrapper class that could require the classes and instantiate the objects and make them accessible. Then you could probably autoload such library and use it as needed.
I would recommend that you at least tried to have them fit in the CI way, as moving forward this will make you life much more easy. I've been in kind of the same position and learned just this along the way.
require_once(PHYSICAL_BASE_URL . 'system/application/controllers/abc.php');
$report= new abc();
Next use the function detail in abc contoller:
$mark=$report->detail($user);
If you're just starting to use CodeIgniter, maybe you ought to check Kohana (http://kohanaframework.org/). It is very similar to CodeIgniter in many ways but it loads classes in the normal way (using new ClassName()) so Netbeans' autocompletion features should works normally.

Categories