Custom load class - php

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

Related

The management of files and folders in codeIgniter can be made from controller or model?

I want to verify if file/folder exists on my CI system (not system folder), can i do it from controller or I need create a method on my model to do it?
It's basically a matter of choice. You can define your function in both your model and controller. But if you want to use it over and over from different controllers, then better define a new model and put that function in it. I'd define model which is to be used by many controllers that is having some common-general methods. Just to keep things apart and maintaining them later. Helpers are also a way to keep things separate.
You have got many way for this :
You can create an helper and put your function like verify()
You can create a private function into your controller like
private function _verify() { }
enjoy !
Best to add this function in a codeigniter helper or in a library class(if you are fan of OOP). Load this automatically with autoload.php and call from wherever you need it.

codeigniter shorten method

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.

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();

How to call controller function in other php file - codigniter

First just look at my code than i will explain my problem.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Category extends CI_Controller {
function Category()
{
parent::__construct();
$this->load->model('category_model');
}
public function index()
{
}
public function _left()
{
echo "side";
$data['all_categories'] = $this->category_model->getallcategory();
$this->load->view('include/left', $data);
}
}
this is my category controller, i have a left() function in which i listed all category in the left bar of my website.
I just want to know that how can i show left() function data in another php file ?
You really shouldn't have a function you would call repeatedly or from another file in a controller... typically you would want to put them in a Helper library or as a Plugin...
For the function you have created, I am wondering if you know that you can have a view, that calls other views. For example, you have a template view that would load the header view, the view referenced in $data from your controller, and your left view, etc...
I would read more about MVC and how it's setup and how you lay out your files a bit more as it will save you a huge headache and some messy code.
Best of luck!
you call create one function and call many view from this.
or create one main view and other top,left ,right,center view and load from main view
I would consider writing a helper and autoloading it in your config file
For more information about creating helpers go here CodeIgniter: Create new helper? or check CodeIgniter's user guide.
DISCLAIMER: THIS IS A RANT
I know everyone is trying to be "helpful" but please, please, please stop using "helper" classes and "helper" files. Models get there own data, they persist their own data, in short, they do the helping!!
Ex:
DO NOT create a file called "userhelper.php", just put the methods in a file called "user.php". This is called a model. You might say at this point, "What if I need to share the model with another part of my project or just somewhere else but make it look different or something or whatever??" That's where you would use a viewmodel. The viewmodel has no persistence information in it, in fact it should be significantly different from the model enough to justify its own existence.
To wrap up, just put the CRUD into the model itself. Don't build "managers" or "helpers' or whatever the hell you want to call them.
And I don't care if CodeIgniter encourages this with it's "helper" framework. It's still WRONG and is not OOP. You will end up writing messy code and duplicating effort all over the place.

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