59190

cakephp requestAction method View file

Question:

How can i call a requestAction method from my view, to specific controller, which returns me results on basis of conditions i mention?

Thanks !

Answer1:

Generally speaking, using requestAction is not very performant, because it begins a whole new dispatch process -- in essence your application is handling two requests for every one request made by a user. For this reason, you want to avoid using it as much as possible. That being said, requestAction has its uses, and you can mitigate the performance hit using view caching.

It's hard to gauge precisely what you want to do with requestAction, but the fundamental concept is you're asking CakePHP to process another request, so you can simply pass requestAction any URL your app would handle if it were typed into a browser's address bar (excluding the protocol and hostname). If you wanted to retrieve a collection of blogs managed by your application:

$blogs = $this->requestAction('/blogs/index');

You can also invoke requestAction by passing it an array of route components, in the same way as you might pass them into HtmlHelper::link. So you might retrieve the collection of blogs thus:

$blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index');

Insofar as filtering the result-set returned by requestAction, it again is done by passing the conditions as part of the URL or route components:

$blogs = $this->requestAction('/blogs/index/author_id:1234'); // or $blogs = $this->requestAction('controller'=>'blogs', 'action'=>'index', 'author_id' => 1234);

Note that if you want your requested action to return values, you need the action to handle requests differently than standard action requests. For the BlogsController::index action I've been referring to above, it might look something like this:

class BlogsController extends AppController{ function index(){ $conditions = array(); if ( isset($this->params['author_id'])){ $conditions['Blog.author_id'] = $this->params['author_id']; } $blogs = $this->Blog->find('all', array('conditions'=>$conditions); if ( isset($this->params['requested']) && $this->params['requested'] == 1){ return $blogs; } else{ $this->set(compact('blogs')); } } }

The if statement checking the presence and value of $this->params['requested'] is the key part. It checks whether the action was invoked by requestAction or not. If it was, it returns the collection of blogs returned by Blog::find; otherwise, it makes the collection available to the view, and allows the controller to continue onto rendering the view.

There are many nuances to using requestAction to arrive at the specific results you require, but the above should provide you with the basics. Check out the links posted by <strong>dogmatic69</strong> for additional documentation, and of course there are numerous stacko questions on the subject.

Feel free to comment with any follow-ups! Hope this helped.

Answer2:

$comments = $this->requestAction('/comments/latest'); foreach($comments as $comment) { echo $comment['Comment']['title']; }

put this code in your ctp file

Recommend

  • List all tables in cakePHP 3.x
  • cakephp get schema for list of tables
  • Store ArrayList into SharedPreferences
  • How to: CakePHP logging in without password?
  • Regex: Replace every Comma with Tab Not within quotes
  • Cakephp Form Helper
  • c# devexpress piechart series point color change
  • Ansible: setting user on dynamic ec2
  • How to obtain service credentials for a service instance created on IBM Bluemix without binding the
  • Raise Session_OnStart event from custom ASP.NET SessionStateProvider class
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • PHP problems with current url
  • How to autoplay video in angularjs?
  • Laravel Eloquent integration in Codeigniter
  • How to only store 3 values for a key in a dictionary? Python
  • PHP: Get HTTP Protocol Version (HTTP/1.1 vs HTTP/2)
  • UIImagePickerControllerDelegate Methods Not Called When Delegate Not UIViewController
  • Base64 as method of sanitizing user input for Mysql
  • How to open multiple instances of a program in Linux
  • Efficient algorithm to find additions and removals from 2 collections
  • KnockoutObservableArray with typed elements in TypeScript
  • what makes a request a new request in asp.net C#
  • msbuild create itemgroup from property group
  • System.InvalidCastException: Specified cast is not valid
  • How to define and use opencv mat of user type
  • Django rest serializer Breaks when data exists
  • Meteor: Do Something On Email Verification Confirmation
  • Read a local file using javascript
  • Cannot resolve symbol 'MyApi'
  • How to rebase a series of branches?
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Projection media query: browser support and workarounds?
  • Azure Cloud Service Web Role web pages do not load
  • Align navbar back button on right side
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Window Size for Mac application
  • what is the difference between the asp.net mvc application and asp.net web application
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • using HTMLImports.whenReady not working in chrome
  • How to push additional view controllers onto NavigationController but keep the TabBar?