Global variable in Zend Controller - zend-framework

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 ?

Related

How to use preDispatch variables in my controller

I am creating custom plugin in zend framework using Zend_Controller_Plugin_Abstract in the disptach method i am using the following code
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
global $serversetting;
$serversetting = 'S3Server';
}
Now I want to use the value of $serversetting inside my controller
How Can i use them??
Thanks In Advance !!
class MyclassController extends Zend_Controller_Action
{
private $_serversetting;
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$this->_serversetting = 'S3Server'; //set
}
[...]
public function otherAction() {
[...]
$foo = $this->_serversetting; //get
[...]
}
}

Can you use a class inside of a function in C#

I was wondering if I could use a class inside of a function. I would the call the function in another file using using filename.FuntionName(); I made the class public so that it would more likely cooperate. Also, would using filename.FunctionName(); call the function from the other file, or just use it as a resource? Well, here is the code:
namespace file
{
public void file()
{
public class file
{
/*function
code*/
}
}
}
You cant declare classes inside functions like Java , however you can use var to create an anonymous type that are not visible outside the function
void file()
{
var file=new {
path="path",
size=200};
Console.WriteLine(file.path+" "+file.size);
}
I suspect what you're looking for is a static method.
namespace SomeNamespace
{
public class SomeClass
{
public static void CallMe()
{
Console.WriteLine("Hello World!");
}
}
}
Then you can call SomeNameSpace.SomeClass.CallMe() from elsewhere without having to create a new instance of SomeClass.
The correct way to declare a function:
namespace Company.Utilities.File
{
public class File
{
public File(string filename)
{
Filename = filename;
}
public string Filename { get; private set; }
public void Process()
{
// Some code to process the file.
}
public static void ProcessFile(string filename)
{
File file = new File(filename);
file.Process();
}
}
}
The point with the long namespace name is that it should be unique even when using assemblies from other companies (Third Part assemblies)
If you want to call the class like it was a function you should create a static function (like ProcessFile) then you can call File.ProcessFile(filename);

Calling Partial Methods in C#

i was recently digging on new partial methods in c#3.0, i understood the use of partial class, that it could be chunked into multiple file one contain the definition and other declaration, but i wanted to know,i created a partial class like below:
in class1.cs
partial class A
{
partial void Method();
}
in class2.cs
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
}
now in class3.cs
class MainClass
{
static void Main()
{
A obj = new A();
obj.Method(); //Here i cannot call the "Method" method.
}
}
then whats the use of creating partial method, i read on MSDN that, at runtime, compiler compiles the class into one, in that case compiler should be getting the "Method" method implementation also, then why it dont allow me to call the "Method" method in the main method, can anyone correct me if i am wrong, and tell me why i am unable to call this partial method in main.
From MSDN
No access modifiers or attributes are allowed. Partial methods are
implicitly private.
It's a private method, so you can't call it from main.
You can call a partial method inside the constructor where the method is defined.
For example
public partial class classA
{
partial void mymethod();
}
public partial class classA
{
partial void mymethod()
{
Console.WriteLine("Invoking partial method");
}
public ClassA()
{
mymethod();
}
}
public class MainClass
{
static void Main()
{
ClassA ca=new ClassA();
}
}
That's it..now execute your code and see the result..
OutPut
Invoking partial method
Yes, we can't call it from Main(). Problem is not Partial method problem is method without specifier in a class is Private and private method can be called inside the class only.
Try creating a new public method in Partial class:
partial class A
{
partial void Method();
}
partial class A
{
partial void Method()
{
Console.WriteLine("Hello World");
}
public void Study()
{
Console.WriteLine("I am studying");
Method();
}
}
class MainClass
{
static void Main()
{
A obj = new A();
obj.Study();
}
}

Strange behavior of the Controller_Plugin

In this example I want increase the session variable called "test" by one each time it enter to the controller. If comment the content of the method preDispath works fine but with this precise example, the session variable "test" increase in 3 or 5 each time.
I use Zend Framework 1.11.4
Why??? I hope you understand my question.
Remember this example is only to show the strange behavior of the method preDispatch
My plugin
class App_Plugins_Permisos extends Zend_Controller_Plugin_Abstract{
public function __construct(){}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$request->setModuleName('default');
$request->setControllerName('index');
$request->setActionName('index');
}
}
My bootstrap
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSession(){
Zend_Session::start();
}
protected function _initPlugins(){
$this->bootstrap('frontcontroller');
$this->frontController->registerPlugin(new App_Plugins_Permisos());
}
}
My Controller
class IndexController extends Zend_Controller_Action{
public function init(){}
public function indexAction(){
$s = new Zend_Session_Namespace('test');
if(isset($s->test)){
$s->test++;
}else{
$s->test = 1;
}
Zend_Debug::Dump($s->test);
die();
}
}
Thanks a lot
Try it putting in .ini resources.frontController.plugins.foo = "My_Plugin_Foo"
If works tell me! Tnks

Zend Framework: How to render a different action?

I have two actions that are essentially identical, but need different URLs. Normally I would use _forward() to render the other action:
class MyController extends Zend_Controller_Action
{
public function actionOneAction()
{
$this->_forward('action-two');
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
However, I have some code the is happening in preDispatch() that I only want to execute once:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->_forward('action-two'); //this won't work because preDispatch() will get called a second time
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
So I thought maybe I could simply call the function directly, like this:
class MyController extends Zend_Controller_Action
{
public function preDispatch()
{
//execute this only once before actionOne or actionTwo, but not both
}
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
}
public function actionTwoAction()
{
$this->view->foobar = 'foobar';
}
}
But now Zend Framework is complaining about not being able to find the action-one.phtml view script. I don't want to render actionOne's view script. I want to render actionTwo's view script. What do I need to do?
Using render() seems to do the trick:
public function actionOneAction()
{
$this->actionTwoAction(); //execute the same code as actionTwoAction()
$this->render('action-two'); //renders the same view as actionTwoAction()
}