0
votes

I am using CakePHP 1.2. I am studying the logic of CakePHP code written by someone else. I am examining the following controller:

<?php
// For serving up images or other files.
class ImagesController extends AppController
{
    function __setupDir($destination)
    {
        ..........
    }
    function view()
    {
        ..........    
    }
}
?>

I know CakePHP has conventions that define how specific actions are called when requests match a URL. For example, given my ImagesController above, I would need to have this file:

app/views/images/view.ctp

Then I would need to point the browser for example to something like this:

http://localhost/myapplication/images/view

That should execute the "view()" function defined for the ImagesController controller mentioned above. This is how the CakePHP 1.2 conventions work.

According to https://book.cakephp.org/1.2/en/The-Manual/Developing-with-CakePHP/Controllers.html, "actions are controller methods used to display views. An action is a single method of a controller." In my case, the "view()" function is an action, but in the application from what I see it is not being used to display a view. In the code I am studying, what I see is that sometimes when pictures or images are loaded in the website, the "view()" function of the ImagesController controller is executed. I am trying to understand exactly what triggers this "view()" function. I know it is not happening when visiting http://localhost/myapplication/images/view. Is there an alternative way to execute a controller action in CakePHP in general, or particularly in CakePHP 1.2? I just need to know and find the code that triggers this "view()" function. Thank you.

2
Log a backtrace and save yourself a lot of time Debugger::log('foo'). book.cakephp.org/1.2/en/The-Manual/Common-Tasks-With-CakePHP/…ndm
Can I use it from a file such as core/cake/libs/controller/image_controller.php?Jaime Montoya
There should be no such file in the core folder, but generally yes, you can run that code in a controller.ndm
Thanks @ndm. The result of CakeLog::write('debug', Debugger::trace()); was: Object::dispatchMethod() - CORE\cake\libs\object.php, line 128 Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 247 Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 211 [main] - APP\webroot\index.php, line 99. It does not really give me the hint I need to figure out what triggers this "function view()" I mentioned in my question. I am trying to understand where in the code "view()" is triggered or executed. I wanted Debugger::trace() to return something not only from the CakePHP core code, but from app.Jaime Montoya
That method uses PHPs debug_backtrace(), and should only exclude call_user_func_array and trigger_error. So if there are no further entries, chances are that there is no further code involved. You can try to get a formatted stacktrace from an exception object ($ex = new Exception(); $trace = $ex->getTraceAsString()), but the results will probably be similar.ndm

2 Answers

1
votes

When you open the link that you give, they write in a "danger" (red) panel: "This document is for a version of CakePHP that is no longer supported. Please upgrade to a newer release!"

In other words it means: Please... stop what you do and start with the latest version of cakephp (now it's 3.5)

A big part of what you will learn with cakephp 1.2 are probably deprecated and surely not recommanded with new PSR.


Sorry bro! for your question actions are not only controllers methods used to display view, if you set $this->autoRender to false, your actions will not need views

For example with an ajax action or when you will launch a simple method that don't need to return a visual information / output (to render a view) but just a single message in your current page). Example or method that don't need output (view) but only get the result

  • deleting something (a group, a product, a client, ...)
  • logout a user
  • check if the user use an authorize ip adress to connect,
  • ...

And if i understand your question, view and function view() are not the same. views are the output (what you see on the screen) and wiew() is a public function used to display the detail of an element in your application (a user, a group, a product, an event, a course, ....) When you create an application, for each elements / entity you will be supposed to have a controller with a least CRUD functions,

  1. C for create / insert a new entity (generally called add() method),
  2. R to read / select one entity (called view() method) or a list of entity (called index() method),
  3. U to update an element (method called edit()) and
  4. D to delete (method called delete())

It means that in your Users controller (for example) you will find at least, index(), add() edit(), view() and delete() methods and additionnaly others public functions (needed for your application or this entity) like login(), logout(), changePassword(), resetPassword(), and callbacks methods like beforefilter(), beforerender(), afterfilter() that let you insert some logic before or after controllers actions ...

Note 2 things:

  1. Views are render inside a layout, (the layout is the part of your code that don't need to change from a page to another one, and the view is the dynamic information)
  2. Cakephp have a code generator (bake) that create for you with a single command line, all your controllers, view, models, ...

Hope it helps

0
votes

I used Debugger::trace() to examine and understand the code. This is how my "view()" controller action is executed for example:

ImagesController->view('blue-theme', 'timeleft1.jpg')