0

I have PHP experience but first time working with CakePHP 4.

"cakephp/cakephp": "~4.4",   
"friendsofcake/crud": "^6.1.1"

The project is using the friends of cake CRUD plugin.

A controller already had a view method in it and an associated view.php template file.

What I want to do is create an edit method and make the edit method use the exact same view.php file. I don't want a duplicate edit.php file which is an exact copy of the view.php file.

Below is part of the controller code where you can see both the relevant parts of view and edit methods

public function view($id)
{

        //Some other code
    
        $this->set(compact('fields'));
    
        return $this->Crud->execute();
    }
    
    public function edit($id)
    {
    
        //some other code here
    
        $this->set(compact('fields'));
    
        return $this->Crud->execute();
    }

Even if using one template for view and edit is a bad idea I would still like to understand and know how could I use/open a specific template programmatically in a controller. You can see in the comments I found various solutions and recommendations that don't really work in this case.

I tried to debug the Crud execute method and $view towards the end is always edit when I try 1., 2. or 3. from the edit method.

    public function execute(?string $controllerAction = null, array $args = []): ResponseInterface
    {
        $this->_loadListeners();

        $this->_action = $controllerAction ?: $this->_action;

        $action = $this->_action;
        if (empty($args)) {
            $args = $this->getController()->getRequest()->getParam('pass');
        }

        try {
            $event = $this->trigger('beforeHandle', $this->getSubject(compact('args', 'action')));

            $response = $this->action($event->getSubject()->action)->handle($event->getSubject()->args);
            if ($response instanceof ResponseInterface) {
                return $response;
            }
        } catch (CrudException $e) {
            $response = $e->getResponse();
            if ($response !== null) {
                return $response;
            }

            throw $e;
        }

        $view = null;
        $crudAction = $this->action($action);
        if (method_exists($crudAction, 'view')) {
            $view = $crudAction->view();
        }

        return $this->_controller->render($view);
    }

I tried using the following in the edit method and none of them change the template to view.php. I always get an error saying missing template. Make sure there is an edit.php file

    //$this->viewBuilder()->setTemplate('view'); //1.

    //$this->Crud->action()->setConfig('scaffold.view', 'view'); //2.

    //$this->render('view'); //3.
  • Have you tried this way of setting the [view](https://crud.readthedocs.io/en/latest/actions/view.html#id1) ? `$this->Crud->action()->view('my_custom_view'); return $this->Crud->execute();`or use `beforeFilter` hook, and map correct view in the [controller](https://crud.readthedocs.io/en/latest/configuration.html#action-configuration) ? – Sergey Ligus Jul 31 '23 at 14:15
  • If the view template is under the same name as a controller you can simply use ```// Render the view in templates/[Controller Name]/view.php return $this->render();``` – Miheretab Alemu Jul 31 '23 at 14:18

0 Answers0