Access controller from action helper - zend-framework

Is it possible to access controller from action helper?
class Zend_Controller_Action_Helper_UserLimit extends Zend_Controller_Action_Helper_Abstract
{
public test()
{
$this->getController(); // get controller which initiated the helper
}
}

Yes, it is possible:
$controller = $this->getActionController();

Related

zendframework action helper to display form

I want to have a login form when i click www.example.com/login with the help of action helper .
My controllers/helpers directory contain the file name
the class Login.php is
In my views/login/index.phtml i m calling it like this
<?= $this->loginForm; ?>
Login.php
<?
class Zend_Controller_Action_Helper_Login extends
Zend_Controller_Action_Helper_Abstract
{
function Login($a)
{
//Consists of form elements
}
}
?>
I get the following error ---------
"Helper "login" does not support overloading via direct()"
How to display the form and what is the error about ?
I was able to get it right this way .
http://geekabyte.blogspot.nl/2012/08/understanding-zend-frameworks-plugin.html
in the Bootstrap.php
public function _initLoader()
{
//For configuring Zend Application to be aware of the Controller Action Helper
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
}
The LoginController.php
class LoginController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->_helper->login->scream();
}
}
In the controller/helpers/Login.php
class Zend_Controller_Action_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
Public function scream()
{
echo "Here please help!!!";
}
}

Accessing my class from another form

I've initiated a class within my form like so:
namespace StartScreen
{
public partial class SetupScreen : Form
{
Battleship myBattleship;
Control myObject;
public SetupScreen()
{
InitializeComponent();
myBattleship = new Battleship();
//Create Class Object
}
}
}
I want to access the class object myBattleship in another form, I can pass it through however when I do I can't make it public so it can be accessed everywhere from the new form. How would I go about doing this?
in Main Form:
public class Battleship
{
//define what you want///
}
public myBattleship = new Battleship();
in Form you want to use myBattleship:
MainForm.myBattleship;

Global variable in Zend Controller

class UserController extends Zend_Controller_Action
{
protected $varGlobal;
public function oneAction()
{
$this->varGlobal=0;
.........
}
public function twoAction()
{
$temp=$this->varGlobal;//temp return null;
}
}
how do i keep value of varGlobal after excuted oneAction()?
Thanks!
You could also try static properties :
class UserController extends Zend_Controller_Action
{
protected static $varGlobal;
public function init(){
self::$varGlobal=0;
}
public function oneAction()
{
self::$varGlobal=15;
.........
}
public function twoAction()
{
$temp=self::$varGlobal;//temp return 0,don't change value after excute oneaction();
}
}
Well, $this->varGlobal persists as long as you call twoAction method on same object. For every request new object gets created. so if your next request goes to twoAction, one action won't gets fired.
In order to set the variable for every object, you could use init function and initialize variables there.
Why don't use Zend_Controller_Action::_setParam() and Zend_Controller_Action::_getParam() ?
or Zend_Registry ?

ASP.NET MVC 2 A problem with OnActionExecuting method

I have a controller called "SomeController". I want to check if the user is logged in or if has persissions to execute any action in that controller. To do so, I read that article http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/ and I've written my own class (a test):
public class BaseFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
//here will be checking the user permissions if he's logged in
}
}
[BaseFilter]
public class SomeController : BaseController
{
...
}
but as You can understand it makes an infinitive loop when I want to run any action from that controller. So, how to cope with that ?
You can apply the action filter on the relevant methods instead of at the class level.
Personally I would name this something like Authorize and then apply it to the controller methods that require authorization.
[Authorize]
public ActionResult Index()
{
// Do stuff
}

How to get resource in controller action?

How to get resource in controller action?
Resource db was initialized in application.ini.
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// I want db resource here
}
}
Try and see if this works:
$this->getFrontController()->getParam('bootstrap')->getResource('db')
UPDATE : While this solution works, it is NOT a recommended
practice. Please, read comment by
#Brian M. below.
You can use Zend_Registry. Initialize the database connection in the bootstrap and store it in the registry:
// set up the database handler
// (...)
Zend_Registry::set('dbh', $dbh);
Then you can retireve it from anywhere else:
$dbh = Zend_Registry::get('dbh');
In answer to a similar question on Nabble, Matthew Weier O'Phinney (Mr Zend Framework 1) suggests using this form:
$this->getInvokeArg('bootstrap')->getResource('db');
So, in the context of this question, it would be something like:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// db resource here
$db = $this->getInvokeArg('bootstrap')->getResource('db');
}
}