Question

I am Using the Zend Framework. As a design pattern i am using the state design pattern.

Now as you may know, Zend Framework let's you create custom controllers, which can be used to respond to Ajax requests. In my example i have the following ajax request

    function getResponse(name){
        $.ajax({
            dataType: 'json',
            data: {button: name},
            url: 'motor/ajaxtest',
            type: 'post',
            success: function(response)
            {
            }
        });
    }

The function getResponse is called every time a specific button is pressed.

public function ajaxtestAction()
{
    $input_in = $this->getRequest()->getParam('button');

    $Lok = new Lok();
    $this->_helper->viewRenderer->setNoRender();

    $text = array($Lok->getMotorState());
    $phpNative = Zend_Json::encode($text);
    echo $phpNative;
}

The Code above is my custom response to the ajax request. I want to pass on the name of the pressed button to $Lok = new Lok(); so i can use it in the "Lok" model Class without creating a new instance of The controller in the "Lok" class

Is there anyone who might be able to help me ?

EDIT-----------------------------------

Here's my Controller :

class MotorController extends Zend_Controller_Action
{
public function init()
{
}

public function indexAction() 
{ 
}

public function ajaxtestAction()
{
    $input_in = array($this->getRequest()->getParam('button'));
    $phpNativ1 = Zend_Json::encode($input_in);
    echo $phpNativ1;

    $Lok = new Lok();
    echo $input_in;
    $this->_helper->viewRenderer->setNoRender();

    $text = array($Lok->getMotorState());
    $phpNative = Zend_Json::encode($text);
    echo $phpNative;
}
}

Here are my Jquery functions :

$(document).ready(function(){

    $("p").click(function(){
        $(this).hide();
        $("input[name=State]").val('Forwards');
    });

    function getResponse(name){
        $.ajax({
            dataType: 'json',
            data: {button: name},
        url: 'motor/ajaxtest',
        type: 'post',
        success: function(response)
        {
        }
        });
    }

    $("button[name=on]").click(function() {
        var d_response = getResponse('on');
    });
});

And this is my Lok.php file :

class Lok
{
private $newMotor;
private $newTimer;
private $newSpeaker;
private $mySession;
private $motorState;
private $input;

public function __construct()
{
    //Method instances
    $newMotor = new Motor();
    $newTimer = new Timer();
    $newSpeaker = new Speaker();    
    $this->motorState = $newMotor->getMotorState();

    // Declaring the Session
    $mySession = new Zend_Session_Namespace();
    $mySession->s_motorState = $this->motorState;
}

public function __get($mySession)
{
    return $this->mySession;
}

public function __set($motorState, $mySession)
{
    $this->$mySession->s_motorState = $motorState;
}

public function getMotorState()
{
    return $this->motorState;
}

public function playSound($soundNumber)
{
    echo "Playing sound";
}

public function resetTimer()
{
    echo "Resetting timer";
}

public function setInput($input_in)
{
    $this->input=$input_in;
}
}
Was it helpful?

Solution

As i've stated previously you should get the button name by calling the requests post data, this is done by $postData = $this->getRequest()->getPost()

Then, to get the output into your Model, inside your model class you would create a property as well as setter and getter method for it.

class Lok {
    protected $button;
    public function setButton($btn){}
    public function getButton(){}
}

And then it becomes as easy as doing something like

$lokModel->setButton($postData['button'])

OTHER TIPS

First of all, thanks for posting your solution.

I tried to implement your solution but unfortunaly it didnt work.

So after a good night sleep, i looked at the problem again. I think the problem is, that $postData = $this->getRequest()->getPost() or $postData = $this->getRequest()->getParam('button') is executed in the response itelfe.

<pre>string(2) &quot;sr&quot;
</pre>["Not Moving"]

This is what the JSON response looks like in The Google Chrom debugger. If you'r familliar with this Google Chrome debugger you know what i mean.

Now the button name that i want is in between the &quot tags the only problem is, getting it out of there. and being able to use it before the response is triggerd. I also tried getPost() and getParam('button') in the init() and indexAktion() Methods in the Controller but it still didn't work

public function init()
{
       postData = $this->getRequest()->getPost()
       $lokModel->setButton($postData['button'])
}

public function indexAction() 
{ 
       postData = $this->getRequest()->getPost()
       $lokModel->setButton($postData['button'])
}

Any other ideas ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top