0
votes

I am confused about the distinction and treatment of the index() and view() functions inside CakePHP 2.4.

Specifically, I am trying to modify a 3rd party API authored in CakePHP 2.4

Consider this Controller:

class EventsController extends AppController {
<snip>
  public function index() {
    if ($this->request->params['named']) {  
       $conditions = $this->request->params['named'];
    } else {
      $conditions = array();
   }
}

This allows me to construct a URL like http://myserver/api/events/index/StartTime >=:12/EndTime <=15.json and the StartTime and EndTime get passed into conditions when I do a find.

That's great.

Now that same Controller has this additional function:

public function view($id) {

}

This function seems to be called when I invoke a url like http://myserver/api/events/1234.json

I am trying to extend view with more parameters, just like index. For example, I'd like to invoke:

http://myserver/api/events/1234/MonitorId =:3.json and MonitorId =:3 gets passed as a parameter to handle in view. I don't want to add these into the function definitions - it can be anything.

If I try and do this, I get a "Controller" not found, but this same approach works in index()

Can someone help me achieve my goal and help explain what is going on?

(IF it helps, the full code of the controller is here)

2
You're just using ZoneMinder, you're not one of the authors, right?ndm
I'm a contributor to ZM and for better or worse, maintain the API because no one else does - and also developer of zmNinjauser1361529
I see... was just wondering whether there's a preferred way to report possible security issues, but I just found github.com/ZoneMinder/zoneminder/issues/…, which I guess is the way to go.ndm

2 Answers

1
votes

As I can see, you are using CakePHP RESTful routing routes.php

Here,

  • index is used for listing all the resources e.g. all blog posts. URL /events/index is getting mapped to index method in controller automagically.
  • view is used for showing a specific resource which can be identified by some unique identifier like id or uuid e.g. specific blog post. URL /events/<some_identifier> gets mapped to view method

What you need to do is modify view definition to read all query string parameters that you pass in URL and call Model accordingly. By default. routes parses the first string after events in URL to resource ID and passes it as first argument to view method in controller. This is scaffolded code.

You can use named parameters $this->request->params['named'] in your view definition and construct your queries. No hard-coding.

0
votes

It turns out that I need to explicitly call the view action to pass it more parameters.

So api/events/view/218245/AlarmFrames >=: 80.json works as intended and then I can get the parameters with $conditions = $this->request->params['named'];