Using Variables in layouts using Zend Framework - php

I am trying to learn how to use the Zend Framework and ive ran into trouble. Im trying to place the current users name in the header of the application (displayed on every page), specifically /layouts/scripts/default.phtml.
The MVC architecture is very new to me and confusing me greatly. I do not want to have to place the logic to display this username in the controller every time (this is probably the wrong way to do it anyway), so where would I place the code to assign this variable if not in each controller?
Cheers

This is the kind of thing that action helpers were designed for. A full tutorial on them is a bit beyond the scope of SO, but there are several good tutorials available.
Start with the Zend Framework Documentation and then take a look at Mathew Weier O'Phinney's tutorial and also this one by Rob Allen.
The issue with using a base controller for this kind of thing is that the resources are loaded regardless of wether your controller needs them or not, whereas action helpers are loaded only if needed.
I almost forgot the excellent ZendCasts have a video on action helpers.

You want a base controller and to assign that in the preDispatch method:
class MyApp_Controller_Action extends Zend_Controller_Action {
public function preDispatch() {
parent::preDispatch();
Zend_Layout::getMvcInstance()->assign('username', getCurrentUserName());
}
}
Then extend your own controllers with that new class:
class MyApp_Module_ActionController extends MyApp_Controller_Action {
}
Then in your layout view:
echo $this->layout()->username;

First, read the manual, and than try to accomplish something like this:
class BaseController extends Zend_Controller_Action {
public function preDispatch() {
// your logic to show the user name goes here
}
}
class SomePageController extends BaseController {}
class SomeOtherPageController extends BaseController {}
This will most likely solve your problem.

Related

How to load another controller throughout all the controllers in ZF project?

I have a code snippet that checks user logins and gets navigations and etc throughout the whole site. I have a few controllers and I want that snippet to be in all of them. I get tired of changing all of them one at a time whenever I change something. In one of my other projects I saw it somehow extended to the other controllers using this portion
class MyController extends Zend_Controller_Action
and it looked like
class MyController extends MySnippet_Controller_Action
or something of that sort, and it worked. I however can't get that to work.
What you're looking for is likely a frontcontroller plugin registered against init() or more likely predispatch(). In Zend Framework extending Zend_Controller_Action is frowned upon and in truth is not usually required.
these resources may help you develope the plugin(s) you need:
Understand ZF plugins pt 1
Understand ZF plugins pt 2
and of course the reference manual section
Here is the basic form of a plugin:
class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
parent::preDispatch($request);
//truncated
}
}

Creating a re-usable controller component in Lithium

I am currently developing a Lithium application and have come across a function that I have written that I would like to use across multiple Controllers.
I obviously don't want to have the function in each controller. What is the standard way of creating a re-usable component in Lithium?
The lack of search facility on their documentation is making it difficult to find any specifics.
You could try extending controller. Extending controllers is not that bad according to core devs. If that is not and option you could extract your code into a plugin, still some code in controller though.
All you have to do is create a extensions/action/Controller.php and have your controllers extend that.
In your extensions/action/Controller.php
<?php
namespace app\extensions\action;
class Controller extends \lithium\action\Controller {
protected function _init() {
parent::_init();
//add your functionality here
}
}
?>
And then, your controller has to extend the above mentioned base controller: class MyController extends \app\extensions\action\Controller {
I think this is not a Lithium-specific thing. You could either inherit from the Controller and make your own base controller, but you can also create arbitrary classes that hold your functionality. Don't let a framework inhibit you =)
Regarding the documentation: I usually google in the sense of "<keywords> site:lithify.me"

Most efficient way to implement a 'logged-in' check with MVC in PHP?

I know questions similar to this have been asked, but I have been searching through the internet and I can't seem to find exactly what I'm looking for.
The most common answer is to put it in the controller. I liked a particular solution from stackoverflow that had a SessionController and NonSessionController, both extending the main controller but with SessionController checking if the user is logged in before the dispatch.
Does this mean that the controller would look something like this?
class SessionController
{
...
function view()
{
//view thread stuff
}
function post()
{
if loggedin then
{
//post thread stuff
}
}
{
In this situation, it looks like NonSessionController is useless, and that model is only used when every action the controller handles is either strictly for users or non-users, unlike this forum example.
So I guess my question is, is the general concept of the controller above the most efficient way of dealing with login checks when using MVC?
I think the idea would be to have one controller which checks the session and login, and one that doesn't.
I would put the login check in the constructor of the session controller so that way every controller which extends it will check the login.
The session controller would look like
class SessionController
{
public function __construct()
{
if ( ! AuthenticationHelper::isLoggedIn() )
{
// User is not logged in
// Do something, maybe a redirect to login page
}
}
}
Then you can just extend that controller like
class HomeController extends SessionController
{
public function __construct()
{
parent::__construct();
}
public function index()
{
print "This page checks login status";
}
}
I would make a component. If you're writing your own MVC framework then this will be interesting to see how you implement this.
But, basically you need a class to check for session state. But, if you tie yourself to extending a class for logged in and a class for logged out I'd feel you have too much duplicate code. I also just personally don't think its very intuitive. I'd probably wind up losing track of whether or not a controller should extend the Session or NotSession.
I'm actually in the process of writing my own MVC framework and have thought about how I would solve this problem a little. I haven't gotten to where I've actually implemented code so it's more a working theory at this point. :)
Have one controller base class. That class has a property that we'll call $components. Let's make this an array and it can hold the name of classes for things you want to do in a lot of controllers, but doesn't really belong in the controller itself.
Since I'm using the Front Controller design pattern before the action requested is invoked I will gather the array of $components and load the appropriate class file for each entry.
As each $component file is being loaded I will dynamically add that component object to the controller properties. So that a component with name Session might refer to a class named SessionComponent and you can access it in your controller by using $this->Session->do_something() or $this->SessionComponent->do_something()
I kind-of-sort-of ripped the idea from CakePHP. I use Cake as my production PHP framework and a lot of my ideas for the custom built framework I'm working on is, obviously, inspired by Cake.
If you are inside your SessionController, then you shouldn't need to check the loggedin variable for every function, you should do that inside the constructor or the router, if you feel confident enough to manipulate. That way if the user is not logged in, the SessionController file and class would not be loaded at all.
Sorry, no english:
base controller
class Controller{
function handleLogin()
{
if(!Authentication::isLoggedIn())
{
//do stuff - redirect to login page?
}
}
}
someController
class someController extends Controller{
function someAction()
{
//check login
$this->handleLogin();
//do someAction stuff
}
}

MVC Optimization question Framework Zend

Just a quick logical question.
I have 2 Zend Controllers namely Merchant and Account. Merchant Controller has an action called editAction. My question is whether it is possible to have same action in Account as well without duplicating the code. I managed to create a include file for the view but I like to have a best way to manage my code. I currently have an idea of having an Helper class and invoke that helper class to both these Controllers. But I know someone would have got better solution than this. Please help me.
Thank you
The simplest solution would be to extend Zend_Controller_Action into your own base class and put editAction() into that. For example, assuming you have model classes named 'Account' and 'Merchant':
abstract class My_Controller_Action extends Zend_Controller_Action
{
protected $_modelName;
public function editAction()
{
$model = new $this->_modelName();
// Do your editing here.
}
}
class AccountController extends My_Controller_Action
{
protected $_modelName = 'Account';
}
class MerchantController extends My_Controller_Action
{
protected $_modelName = 'Merchant';
}
Keep in mind that this design implies that the code in editAction() would work for both Accounts and Merchants.
I think the best course of action would be to put majority of the logic inside your Models and keep your controllers lean. Your models can extend your own class which would have common operations in it.
You could also write your own class, it doesn't have to be a helper, and use it in the controllers to save the entity:
$saver = new My_Editing_Class();
$saver->edit("account",$this->getRequest()->getPost()); // Editing "account" with the POST data.
You then do your magic inside "My_Editing_Class".

How do I extend the code igniter controller class?

In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DD_Controller extends Controller
{
protected $ddauthentication;
function __construct()
{
parent::Controller();
$this->ddauthentication = "Authenticated";
}
}
?>
My application controller is defined like this:
class Inquiry extends DD_Controller
{...}
The Inquiry class works fine when I extend Controller, but I get a
Fatal error: Class 'DD_Controller' not
found in
C:\development\localhost\applications\inquiry\controllers\inquiry.php
on line 4
When I extend DD_Controller. In the config file I have the prefix defined as such:
$config['subclass_prefix'] = 'DD_';
Any idea of what I'm missing?
TIA
This is a better approach. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open this the file you just created and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}
DD_Controller.php should be in /system/application/libraries/
If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.
In system/libraries/Controller.php below the Controller class:
class Mega_Controller extends Controller {
function Mega_Controller()
{
parent::Controller();
// anything you want to do in every controller, ye shall perform here.
}
}
Then you'll be able to do this in your app controllers:
class Home extends Mega_Controller {
....
Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.
I recommend to avoid "cracking" CodeIgniter core files.
Better use its native extending possibilities and try to fit into them.
The same rule I would recommend for any PHP library / CMS.
This rule has few reasons:
- ability to quiclky upgrade without takint into account thousands of notes where and how was cracked in core files;
- portability;
- possibility to share your code - eg, this will be usable by both you and your friends in case of need, and it will help them to keep their library up to date, the same as you.
In other words, this is much more professional and it pays to you in the future by usability, portability and by update application possibility.
Regarding your personal question...
As for me, there is nothing bad to create your own library with everything you need to extend native CodeIgniter Controller, then load this library in Controller's constructor and you are done. The only thing to make better usability is to give short name to your library.
This way you can even divide what you need in different pieces and put into separate libraries:
WebFeatures
AdminFeatures
etc.
Then you just load needed libraries in your controller's constructor and you are done.
P.S. I know that proposed way does not fit into "right" OOP concept, but in the same time you must never forget about the integrity of the libraries used.
Everything above is just one more view of mine 7-years experience in professional web development, so I hope it will be helpful if not to follow, then at least to take into account.
Regards,
Anton

Categories