zend getrequest ajax - php

if ($this->getRequest()->isXmlHttpRequest()) {
$this->_helper->layout->disableLayout();
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('view', 'html');
$ajaxContext->initContext();
}
how does this actually work... my ajax get page is local.maker/profile/check
i got ajax to work fine but i dont know what to edit from the above...
$ajaxContext->addActionContext('???', 'html');
ps.. i am requesting a json

I use the following code to use AJAX context helper.
In your controller create a preDispatch method to set up your contexts like this:
public function preDispatch()
{
$this->_helper->ajaxContext()
->addActionContext('index', array('json', 'html'))
->addActionContext('anotheraction', 'json')
->initContext();
}
And then in you action methods use:
public function indexAction()
{
if ($this->_helper->ajaxContext()->getCurrentContext() == 'json') {
// ajax code here
} else {
// non ajax code here
}
}
Also in your ajax request you must use the variable format to set the current context, for example
http://www.mydomain.com/index/format/json
to request a json response.
Note: The context switcher automatically disables the layout and view, any view variable set in the controller will automatically be encoded into a json string and sent.
I hope this helps
Kind regards
Garry

Please go through the example . It has everything what you are looking for .
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.ajaxcontext
addActionContext( <action>, <format>);
Can request comment/process/format/json or via query variable . See the example .

Related

Laravel - check if Ajax request

I have been trying to find a way to determine Ajax calls in Laravel but I have not found any documentation about it.
I have an index() controller function where I want to handle the response differently based on the nature of the request. Basically this is a resource controller method that is bound to GET request.
public function index()
{
if(!$this->isLogin())
return Redirect::to('login');
if(isAjax()) // This is what I am needing.
{
return $JSON;
}
$data = array(
'records' => $this->table->fetchAll()
);
$this->setLayout(compact('data'));
}
I know the other methods of determining the Ajax request in PHP but I want something specific to Laravel.
Thanks
Updated:
I tried using
if(Request::ajax())
{
echo 'Ajax';
}
But I am receiving this error:
Non-static method Illuminate\Http\Request::ajax() should not be called statically, assuming $this from incompatible context
The class shows that this is not a static method.
Maybe this helps. You have to refer the #param
/**
* Display a listing of the resource.
*
* #param Illuminate\Http\Request $request
* #return Response
*/
public function index(Request $request)
{
if($request->ajax()){
return "AJAX";
}
return "HTTP";
}
$request->wantsJson()
You can try $request->wantsJson() if $request->ajax() does not work
$request->ajax() works if your JavaScript library sets an X-Requested-With HTTP header.
By default Laravel set this header in js/bootstrap.js
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
In my case, I used a different frontend code and I had to put this header manually for $request->ajax() to work.
But $request->wantsJson() will check the axios query without the need for a header X-Requested-With:
// Determine if the current request is asking for JSON. This checks Content-Type equals application/json.
$request->wantsJson()
// or
\Request::wantsJson() // not \Illuminate\Http\Request
To check an ajax request you can use if (Request::ajax())
Note:
If you are using laravel 5, then in the controller replace
use Illuminate\Http\Request;
with
use Request;
I hope it'll work.
You are using the wrong Request class. If you want to use the Facade like: Request::ajax() you have to import this class:
use Illuminate\Support\Facades\Request;
And not Illumiante\Http\Request
Another solution would be injecting an instance of the real request class:
public function index(Request $request){
if($request->ajax()){
return "AJAX";
}
(Now here you have to import Illuminate\Http\Request)
if(Request::ajax())
Looks to be the right answer.
http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax
For those working with AngularJS front-end, it does not use the Ajax header laravel is expecting. (Read more)
Use Request::wantsJson() for AngularJS:
if(Request::wantsJson()) {
// Client wants JSON returned
}
Those who prefer to use laravel helpers they can check if a request is ajax using laravel request() helper.
if(request()->ajax())
// code
public function index()
{
if(!$this->isLogin())
return Redirect::to('login');
if(Request::ajax()) // This is check ajax request
{
return $JSON;
}
$data = array();
$data['records'] = $this->table->fetchAll();
$this->setLayout(compact('data'));
}
Sometimes Request::ajax() doesn't work, then use \Request::ajax()
Do something like this
public function functionName(User $user): string
{
(string) $message = "";
// check if request is ajax
if(request()->ajax())
{
if($this->isOwner($user->id)){
$message = 'success';
}else{
$message = "You are unauthorized to perform this action.";
}
}else{
$message = 'Wrong server request';
}
return $message;
}
after writing the jquery code perform this validation in your route or in controller.
$.ajax({
url: "/id/edit",
data:
name:name,
method:'get',
success:function(data){
console.log(data);}
});
Route::get('/', function(){
if(Request::ajax()){
return 'it's ajax request';}
});
Most of the answers are working fine. We can also check the request header
request()->header('Accept')=='application/json'
to check the request type

How pass a variable during ajax rendering in cakephp 2.x

I want to pass a variable to my ajax rendering function.Something like this
$this->render('tempcomment/'.$id, 'ajax');
and my tempcomment action is
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}
but rendering is not working.
See below to start with:
public function tempcomment($id) {
// ^ ------ ...
$commentdata = $this->Post->Comment->findByPostid($pid);
// ^--- this is different!
$this->set('commentdata', $commentdata);
}
render is used just to decide what view to use in your controller action to render the action. It does not call the action itself
so if you have a code like this
public function test() {
$this->set('foo', 12345);
$this->render('tempcomment', 'ajax');
}
you are just saying that you want to use the tempcomment view to render the test action but you are not telling cake to actually execute the tempcomment action.
so if you want to call the action you have to do by yourself
$this->tempcomment($id);
$this->render('tempcomment/'.$id, 'ajax');
let me tell you that I don't see any reason to do this and I think that probably there are other ways to do what you are trying to achieve. But I assume you know what you are doing
Try this, its another work around of what you want to achieve:
public functionc ajax_tempcomment($id){ //just access this action
$this->redirect('tempcomment/'.$id);
$this->layout = "ajax";
}
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}

i want to call different php function on different pages but from a single function page

I was creating a webpage that calls a different PHP function which is on a single PHP page.
let me explain with a example: page1 contains this jscript:
$.post("function.php", {
func: "fun1",
fun_var: $('#fun1').val() });
and page2 contains
$.post("function.php", {
func: "fun2",
fun_var2: $('#fun2').val() });
and function page contains
fun1()
{
return $result1
}
fun2()
{
return $result2
}
my problem none of the function is called, and I could not found where I am making mistake can any one please provide me the simple skeleton of this type of problem
You can't call a PHP function from a HTTP request.
You need to pass the function names as request variables either via POST or GET.
In PHP make sure to not execute some weird function (e.g. with a whitelist).
This is how you call a PHP function :
function printHello()
{
print 'hello';
}
printHello();
That's it, if it's in another file and you include it, then you will still call it the same way.
If you want to call a PHP function depending on the user's interaction with the site, THEN you need to use AJAX.
Your code looks like pseudo AJAX and it doesn't really make sense, there is an AJAX example here : jQuery Ajax POST example with PHP
page.html:
$.post("function.php", { func: "fun1", fun_var: $('#fun1').val() });
page.php:
if (isset($_POST['func']) && $_POST['func'] === 'fun1')
{
function fun1()
{
return (isset($_POST['func_var'])) ? $_POST['func_var'] : false;
}
fun1();
}
etc...
Try this!

Is a POST controller suitable in a php mvc?

I am creating a custom MVC style framework from scratch and am at the point where I need to implement the code to control what happens on POST.
At the moment I have a main index.php which acts as a controller and passes data to other controllers such as:
profilecontroller.class.php
forumcontroller.class.php
At the moment I see two options as to where the POST controllers can go ..
First Approach
Firstly for site wide posts such as login that can occur on any page I would use something like this in the very first index.php to redirect all POST to a specific POST controller that then sends the data to a model to be processed:
if($_POST)
//post controller, works on specific form id's
Alternate Approach
The other option I see would be to build the POST identifier into the model construction sections but I don't think this would be very manageable/wise as they'd always be checked and resulting in more loaded code?
Are there any good/simple examples out there?
I'm creating my mvc to be as light as possible so that's my reason for going from scratch.
In a RESTful setup, you would normally have a controller for an object, say news, and then actions such as add, edit, delete etc.
Within your actions, you should then assert what HTTP method should be used to access the method, if one should be. For example:
<?php
class NewsController extends AbstractController {
public function save() {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header('HTTP/1.1 405 Method Not Allowed');
die('Please use POST.');
}
// carry on knowing we're working with a POST request
}
}
Creating a separate controller for POST requests would, as you say, quickly becoming unruly and unmanageable.
If you're looking for a way of handling requests for different HTTP methods within different controller actions, then maybe check out ToroPHP. It's a lightweight (single file) router, where you map a request to a class that's referred to as a handler, and then that handler has methods for different HTTP methods. A quick example:
<?php
require 'lib/torophp/toro.php';
require 'classes/handlers/HomeHandler.php';
$toro = new ToroApplication(array(
array('/', 'HomeHandler')
));
$toro->serve();
And then your HomeHandler would look as follows:
<?php
class HomeHandler {
public function get() {
echo 'Hello, world!';
}
public function post() {
echo 'Try performing a GET request for the home page, buddy.';
}
// and so on...
}
Hope that helps.
This is my default Controller :
<?php
Class Controller_Home{
public $Registery = null;
final public function __construct($Registery){ $this->Registery = $Registery; }
final public function Init($Method=null){
# Quelle action on fait ?
if($Method){
$Split = explode('_', $Method);
$MethodName = 'Action';
foreach($Split as $Splitted){
$MethodName.= '_'.ucfirst($Splitted);
}
if(method_exists($this, $MethodName)){
$this->$MethodName();
} else {
echo '404';
die;
}
} else {
$this->Action_Default();
}
}
final public function Action_Default(){
$this->Registery->Import('Library.Account');
var_dump($this->Registery->Account);
echo 'Default Home';
}
}
As you can see, once you are in Action_Default, you can do whatever you want based on $_GET, $_POST, whatever you want ...
So with this code :
website.com/home/bob/ will use function Action_Bob inside the controller Home (Home::Action_Bob) ... if you see $_POST just put inside Action_Bob this
public function Action_Bob(){
if($_POST){
$this->Action_Bob_Post();
}
// continue
}

How to create modular MVC components in Zend Framework

I've been having problems created modular reusable components in my Zend Framework app. In this case I'm not referring to Zend Framework modules but rather the ability to have a reusable MVC widgety thing if you like. The problems I'm having may be very particular to my implementation, but I'm completely happy to throw it out and start again if someone can point me in the right direction. Anyway, specifics and code will hopefully explain things better and even if what I'm doing is not the best way it should show what I'm trying to achieve:
A simple example is a Mailing List sign up form. I want to include this on several pages of the site which use different Controllers and this presents a few problems in how to process the data and return relevant messages. I don't want to do either of the following as they really smell:
Create a base controller with the form processing in and extend (Bad)
Duplicate form processing code in relevant controllers (Even worse!)
The clean way to go feels to me to create a new Controller to process the mailing list form data, use a View Helper to easily output the form and relevant markup into the desired pages and then redirect back to the page where signup occurred once the form has been processed. However, I'd like to use the form validation provided by Zend_Form, which means I'd need to pass the form object back to the view helper somehow if validation fails but in the same request. I'm currently doing this by setting it as a variable on the view and then forwarding back to the previous page rather than redirecting, which is ok(ish). If validation is ok then I'd prefer to use a redirect back to the original page. I'm having trouble doing this though as I'd like to pass messages back to the component about the state of signup. Normally I'd use the FlashMessenger Action Helper, I could namespace it in this case so messages didn't clash with other page data, but I can't access it from within a View Helper. So currently I'm forwarding in this case too. I'd much prefer a redirect to prevent form resubmissions if a user refreshes the page and to keep the URL clean. I realise I essentially want to have a mini MVC dispatch process within a page and I think that's what the action stack is for? I really don't know much about this though and any pointers would be greatly appreciated. Here's my current code:
Controller:
<?php
class MailingListController extends Zend_Controller_Action {
public function insertAction() {
$request = $this->getRequest();
$returnTo = $request->getParam('return_to');
if(!$request->isPost() || (!isset($returnTo) || empty($returnTo))) {
$this->_redirect('/');
}
$mailingList = new Model_MailingList();
$form = new Form_MailingList();
$returnTo = explode('/', $returnTo);
if($form->isValid($_POST)) {
$emailAddress = $form->getValue('email_address');
$mailingList->addEmailAddress($emailAddress);
$this->view->mailingListMessages = $mailingList->getMessages();
$this->view->mailingListForm = "";
}
else {
$this->view->mailingListForm = $form;
}
$this->_forward($returnTo[2], $returnTo[1], $returnTo[0]);
}
}
return_to is a string containing the current URI (module/controller/action), which is generated in the View Helper. I'd prefer to redirect inside the $form->isValid($_POST) block.
View Helper:
<?php
class Zend_View_Helper_MailingList extends Zend_View_Helper_Abstract {
public function mailingList($form, $messages = "") {
if(!isset($form)) {
$request = Zend_Controller_Front::getInstance()->getRequest();
$currentPage = $request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName();
$form = new Form_MailingList();
$form->setAction('/mailing-list/insert');
$form->setCurrentPage($currentPage);
}
$html = '<div class="mailingList"><h2>Join Our Mailing List</h2>' . $form;
$html .= $messages;
$html .= '</div>';
return $html;
}
}
Getting an instance of the Front Controller in the View Helper isn't ideal but I'd prefer to encapsulate as much as possible.
If I have a form object where validation has failed I can pass it back into the helper to output with error messages. If I have some messages to render I can also pass them into the helper.
In my view scripts I'm using the helper like so:
<?=$this->mailingList($this->mailingListForm, $this->mailingListMessages);?>
If neither mailingListForm or mailingListMessages has been set on the view by MailingListController, it will output a new form with no messages.
Any help is greatly appreciated!
Using ajax seems to be an optimal way. View Action Helper is used only for the first load of the mailing form.
Controller
class MailingListController extends Zend_Controller_Action {
public function insertAction() {
$request = $this->getRequest();
$form = new Form_MailingList();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$mailingList = new Model_MailingList();
$emailAddress = $form->getValue('email_address');
$mailingList->addEmailAddress($emailAddress);
$form = $mailingList->getMessages();
}
}
$this->view->form = $form;
}
}
view script insert.phtml
<?php echo $this->form; ?>
Form class
class Form_MailingList extends Zend_Form {
public function init() {
//among other things
$this->setAttrib('id', 'mailing-list-form');
$this->setAction('/mailing-list/insert');
}
}
View Helper
class Zend_View_Helper_MailingList extends Zend_View_Helper_Abstract {
public function mailingList() {
$this->view->headScript()->appendFile('/js/mailing-list.js');
return '<div id="mailing-list-wrap">' . $this->view->action('insert', 'mailing-list') . '</div>';
}
}
JS file mailing-list.js
$(document).ready(function() {
$('#mailing-list-form').submit(function() {
var formAction = $(this).attr('action');
var formData = $(this).serialize();
$.post(formAction, formData, function(data) {
//response going in form's parent container
$(this).parent().html(data);
});
return false;
});
});
I think the way you've done it is pretty close to what I would do. If you set aside the requirement of wanting to display the Zend_Form error messages in the page, then what you do instead is:
The view helper just displays the form (it doesn't need to take the form object or messages as parameters)
The form submits to your other controller as it does now
The mailing list controller redirects (instead of forwarding) back to the return URL on success
The mailing list controller redisplays the form on its own, along with errors on failure
This makes everything much simpler, the only issue is that if there are any validation errors then the user loses their context and gets a plain old page with the form on instead of where they were. You can then address this (either now or at a later date) by changing the form to submit via. Ajax instead, and rendering the errors via. JS. But this would be a fair amount of work.
OK, I've come up with a solution that I feel happier about and solves some of the problems I was facing. Hopefully, this might help someone out who's facing similar issues. The only downside now is that I'm referencing the Model inside the View Helper. Not loose coupling I know but I've seen this done several times before and it's even recommended in the ZF docs as a way to avoid using the 'action' view helper (which will create a new MVC dispatch loop). On the whole, I think the DRYness and encapsulation is worth it, there's probably some other suitable lingo too.
In order to be able to use a redirect back from my MailingListController but maintain the messages from my model and any form validation errors I need to store them in the session. For messages I'd normally use the FlashMessenger action helper, but as getting hold of this in a View Helper is not best practice, it won't handle my form errors and all it's really doing is saving stuff to the session anyway it's unnecessary. I can implement my own session storage in the Model_MailingList, which I can also use for the form errors. I can then repopulate the form with the errors after the redirect and print out any relevant messages. Anyway, here's the code:
Controller:
<?php
class MailingListController extends Zend_Controller_Action {
public function insertAction() {
$request = $this->getRequest();
$returnTo = $request->getParam('return_to');
if(!$request->isPost() || (!isset($returnTo) || empty($returnTo))) {
$this->_redirect('/');
}
$mailingList = new Model_MailingList();
$form = new Form_MailingList();
if($form->isValid($_POST)) {
$emailAddress = $form->getValue('email_address');
$mailingList->addEmailAddress($emailAddress);
}
else {
$mailingList->setFormErrors($form->getMessages());
}
$redirect = rtrim($request->getBaseUrl(), '/') . $returnTo;
$this->_redirect($redirect);
}
}
I've added a method to my Model_MailingList class; setFormErrors($errors) that I pass the error messages from the form if it fails validation. This saves the error array to the session.
I normally use a base model class that has addMessage and getMessages methods. These just access a protected array of messages. In my Model_MailingList I override these methods to store the messages in the session instead. In the addEmailAddress($emailAddress) method I'm already calling addMessage to say whether inserting the email address to the db has been successful.
Model:
<?php
class Model_MailingList extends Thinkjam_Model_DbAbstract {
private $_session;
public function __construct() {
$this->_session = new Zend_Session_Namespace(__CLASS__);
}
public function setFormErrors($errors) {
$this->_session->formErrors = $errors;
}
public function getFormErrors() {
$errors = array();
if(isset($this->_session->formErrors)) {
$errors = $this->_session->formErrors;
unset($this->_session->formErrors);
}
return $errors;
}
// override addMessage and getMessages
protected function addMessage($message) {
if(!isset($this->_session->messages)) {
$this->_session->messages = array();
}
$this->_session->messages[] = $message;
}
public function getMessages() {
if(isset($this->_session->messages)) {
$this->_messages = $this->_session->messages;
unset($this->_session->messages);
}
return $this->_messages;
}
…
public function addEmailAddress($emailAddress) {
...
// I call this if db insert was successful:
$this->addMessage("Thank you. You have been successfully added to the mailing list.")
}
}
I now don't need to pass any params to the view helper as it can query it's state from the Model directly. $this->view->messenger is just another view helper that converts an array to an unordered list.
View Helper:
<?php
class Zend_View_Helper_MailingList extends Zend_View_Helper_Abstract {
private $_mailingList;
public function MailingList() {
$this->_mailingList = new Model_MailingList();
return $this;
}
public function getForm() {
$request = Zend_Controller_Front::getInstance()->getRequest();
$currentPage = '/' . $request->getModuleName() . '/' . $request->getControllerName() . '/' . $request->getActionName();
$form = new Form_MailingList();
$form->setAction('/mailing-list/insert');
$form->setCurrentPage($currentPage);
$form->setErrors($this->_mailingList->getFormErrors());
$html = '<div class="mailingList"><h2>Join Our Mailing List</h2>' . $form;
$html .= $this->view->messenger($this->_mailingList->getMessages());
$html .= '</div>';
return $html;
}
}
Then in the Form_MailingList class I just need to add an additional method to repopulate the error messages. Although getMessages() is a method of Zend_Form there doesn't appear to be any corresponding setMessages(). You can do this on a Zend_Form_Element however, so I've added the following function to the Form_MailingList class:
Form:
<?php
class Form_MailingList extends Thinkjam_Form_Abstract {
...
public function setErrors(array $errors) {
foreach($errors as $key => $value) {
$this->getElement($key)->setErrors($value);
}
}
}
I can now add a signup form on any page of my site using the MailingList view helper:
<?=$this->MailingList()->getForm();?>
I realise a lot of the problems I was facing was down to a very specific set of circumstances, but hopefully this can help some other people out in some way!
Cheers,
Alex

Categories