codeigniter shorten method - php

I am using codeignitor , currently i have a library named "Common_func" which is autloaded.
and i can call it like this everywhere
$this->Common_func->common_method();
its ok in case of working with controllers and models , but in case of views it makes the HTML ugly ,
I know about the template parsing options , but i do not want to go with that.
is there any posible way to access with a shorten structure.
like this (or similer with least words)
Common_func->common_method();
or better than this.
Thanks.

The best practice is not to use libraries in View files at all, libraries are meant to be used in controllers and models. Helpers are used for views, they are random functions that help you with formatting/code generation and sometimes even more.
Nevertheless, if you really want to keep your library and it's methods you can make "abstract" helpers, that will help you with making view files clean and readable:
common_helper.php
function common_method($arg) {
$ci =& get_instance();
return $ci->Common_func->common_method($arg);
}
That will keep your helpers updated with the changes made in the library.

I found this in codeigniter forum. This link might help you
http://codeigniter.com/forums/viewthread/139788/
or you can shrink a little if your library name is pretty long this way
class SomeClass extends CI_Controller{
function __construct(){
parent::__construct();
$this->lib=$this->Really_Long_Library_name;
}
}
this way you can use $this->lib to access its methods.

Related

use a Helper inside a controller that does not extend AppController

i have created a controller that extends TCPDF to be able to customise a bunch of stuffs ,
also i need to use inside it Helpers .
knowing that i can not have multiple inheritance in php , i tried to create an instance of the view inside the constructor of my new controller to grab the target Helper
like this
class NewPDF extends TCPDF{
public function __construct()
{
$fakeView=new View($this);
$htmlHelper=$fakeView->loadHelper("Html");
# some code ..... parent::__construct()
}
}
it does not work . it gave me weird errors !!!
how can i use a helper inside a controller that does not extend AppController ?
it does not work . it gave me weird errors !!!
It does because you're doing everything totally wrong. That you want to extend a controller with a helper and even throw a view in the mix tells me you have seriously no idea at all how a MVC framework works.
Design patterns in general (MVC is one)
Wikipedia about MVC
CakePHP book explanation of MVC
Random Google article about MVC
Start over here when you understoog the above
At least I'm not going to write in detail what is wrong because like I said, everything is wrong, start with the very basics. The links will explain how to do it right. What you wrote shows a huge lack of knowledge that can't be fixed by a short answer.

Custom load class

Hi I would like to know if it is possible to make custom load class for codeigniter. For my theme switcher system I am making.
I would like to be able to do some thing like this. I am stuck on this issue?
$this->load->controller('folder/file');
$this->load->controller('folder/folder/file');
"controller" is a custom function.
So can do this $data['name] = $this->load->controller('folder/file');
It just would make my life a lot easy if I could do it that way.
I am trying to make my own version of an min hmvc system. I know they always ask why do you need it. Always have to keep explaining it. Lots of people are after this way.
CodeIgniter manage it's class, helper, view, model, database.. loading in
system/core/Loader.php
It's why you can use
$this->load->view();
$this->load->database();
$this->load->model();
$this->load->database();
So, you can extends CI_Loader in
application/core/MY_Loader.php
add controller function and whatever you want
I found a demo in this blog post:
http://www.techsirius.com/2013/01/load-controller-within-another.html
Edit:
If you want to apply to other folder like subfolder, you can add some logic to $file_name
$file_path = APPPATH.'controllers/'.$file_name.'.php';
I think it's not possible to load a controller from an other controller.
You should use Helpers instead :)
http://www.codeigniter.fr/user_guide/general/helpers.html

Short functions from a Codeigniter's library

I got a Tank Auth library installed in my Codeigniter package, the problem is that I don't like how to is_logged_in functions need to be called because it's simply long and not so friendly, as I need to use:
$this->tank_auth->is_logged_in()
Everytime I want to check if user is logged in...
So is there a way to make it shorter? By saying shorter I mean something like $this->logged();?
I would really appreciate if someone could help me.
Thank you.
Head into the tank_auth library and define a new public function:
public function logged(){
return $this->is_logged_in();
}
You can now access it with $this->tank_auth->logged();
If you want to shorten the name of tank_auth, you'll have to rename the class and the filename.
UPDATE:
The more important question is, why are you calling this so many times that it is becoming an annoyance? You should only have to write it once if your code follows the Don't Repeat Yourself (DRY) principle.
Have a look at Phil Sturgeon's blog post entitled Keeping It DRY. He will show you how to write a base controller that all your controllers will inherit from. If you write the login check in the constructor of the base controller, you don't have to write it in every controller.
I would argue against doing this as the method logged() in your instance lacks context. However, if you wanted to do this, you could write a base controller which has a logged() method which ends up returning $this->tank_auth->is_logged_in(). All controllers would inherit from this base controller, which isn't a bad idea to begin with.
If you're using libraries, you could implement a similar pattern in them.
You probably don't want to edit anything in the TankAuth library if you didn't create it as doing so affects the updatability of the library. Instead, you might add a method to your controller called logged and have it reach out to Tank Auth. Although, I would choose a better name for your function as pointed out by a previous answer.
Create or edit your controller base class to have something like this:
function is_logged() {
return $this->tank_auth->is_logged_in();
}
Then you may call it like so: $this->is_logged();

shifting controller function to library in codeigniter

I am trying to put this function ((click_add)) in library so that i can call it from all the controllers. I already have get_ads() function in library. I tried various ways to shift the click_add(id) function to library and call it to view along with get_ads but doesn't work. Please help
function __construct() {
parent::__construct();
$this->load->library('ads');
$this->load->model('MGlobal');
}
public function index(){
$data['banner']= $this->ads->get_ads();
$this->load->view('test',$data);
}
//i want this in library but no luck
public function click_add($ads_id){
$ads_site = $this->MGlobal->getAds($ads_id);
$this->MGlobal->add_ads_view();
redirect($ads_site['url']);
}
//and views is like this
foreach($banner as $k=>$list){
echo anchor('test/click_add/'.$list['bannerid'],'<img src="'. $list['image']. '"/>');
}
please suggest me how do i achieve that with library
It's also important to remember the role of each part of the MVC pattern. In your click_add() method, it looks like you're rendering a view and causing a redirect. These are two things best suited for a controller rather than a library. Rendering views and redirecting are two things that must be a controller's responsibility, and indeed, you won't be able to access them through the URL, which is what you're trying to do here.
If you want to reuse this method across multiple controllers in your site, try creating a MY_Controller core class and extending your controllers from that. That way, any methods you define in the MY_Controller will be available in any controller you subclass from.
Without any specific error messages or a more verbose description of the problem you're having, I'm afraid there's little more help I can give you.

Zend General Functionality

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 .

Categories