use a Helper inside a controller that does not extend AppController - php

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.

Related

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.

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 .

MVC - Can I call more than one (or multiple) controller in class controller?

For projects written in php, can I call more than one (or multiple) controller in class controller? Example in http://img192.imageshack.us/img192/7538/mvc03.gif
ASK: I need to call an action from another controller... And if I do like the picture above, I'm being out-ethics?
Thanks,
Vinicius.
I'm sure that you can do what you want with whichever framework you're using. If you can't do it natively for whatever reason, then you can extend your framework as required.
Having said that, I personally don't like the idea of a controller calling another controller. It seems to somewhat break the MVC paradigm if only from a theoretical standpoint. What I might do instead is build a library class that contains the functionality required and then have both controllers instantiate that class as a member and call the functions required.
For example, using CodeIgniter:
libraries/MyLib.php:
class MyLib
{
public function MyFunc()
{ /* do whatever */ }
}
controllers/ControllerA.php:
class ControllerA extends Controller
{
public function index()
{
$this->load->library('MyLib');
$this->mylib->MyFunc();
}
}
controllers/ControllerB:
class ControllerB extends Controller
{
public function index()
{
$this->load->library('MyLib');
$this->mylib->MyFunc();
}
}
out-ethics? Anywhose... back to reality.
Yes, a controller can call another controller's action. For instance, in cakePHP, this functionality is afforded via requestAction
// pass uri to request action and receive vars back
$ot3 = $this->requestAction('/stories/xenu');
If you're rolling your own, the details of how to implement it will be very specific to your framework.
then you need to modify framework, find place where controller is lounched and add there your second controller.
what framework you are using?
You can do it any way that you want. You don't have to use MVC if you don't want to. However, in MVC you really should only have one controller active at a time. You probably want multiple Views or Models, not another Controller. There is nothing at all wrong in loading, say, a header and footer view for the menu and footer of the site.
If you are building another Controller, then feel that you need to access the functionality of a previous Controller to access its functionality (because it works with a specific / desired Model), then the Model you developed for the latter probably needs to be refactored. IN plain speak, your target Model may be doing too much. Break it up.
You are trying to avoid repeating yourself (DRY) by using calling the methods of a Controller that has already been developed, but in doing so your are creating TIGHT coupling between both controllers! If something changes in the borrowed controller, it will have an effect on the borrowing controller. Not good, Dr. Jones.

How do I set up the following architecture

Sorry if the title is a little vague, I do not know how else to describe it.
I am making my own small framework. Things are going nicely and I am enjoying looking at topics that I usually do not need to check out as 'the magic' does it for me.
My framework is PHP based and I want it to run from a single instance. What I mean by this is the following.
class Controller_Name extends Controller {
public function __construct() {
$this->load->library('session');
$this->load->model('Model_Name');
}
}
class Model_Name extends Model {
public function something() {
if ($this->session->get($something))
// Do something Amazing
}
}
As hopefully illustrated above I want all controllers / Models / Views to share already loaded libraries.
So if a class is loaded in the Controller, I will be able to use it in a view file.
Does anyone know how this done? Can you point me in the direction of an article covering it, what this is called or some php function calls either completely or partly do the job.
As always, any answers are greatly appreciated.
If your aim is to make sure all dependencies inside your classes are resolved, have a look at the Service Containers from the Symfony Dependency Injection Component or the Stubbles framework.
Your controller can implement __get and intercept requests for libraries that have been loaded. Ideally you'd store the libraries in a static field to get the reuse you mentioned you wanted.
Also, to get load to behave like you want I'd personally create a Loader class and create an instance of it in the Controller. Then in the __get magic method I'd make it fetch it from the loader.

Categories