Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

add dispatcher model binding doc #785

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions en/reference/dispatching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,83 @@ before dispatch the action preparing the parameter accordingly:
The above example has been simplified for academic purposes.
A developer can improve it to inject any kind of dependency or model in actions before be executed.

From 2.1.x onwards the dispatcher also comes with an option to handle this internally for all models passed into a controller action.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably say "3.0 onwards" as 2.1.x never shipped.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Phalcon 2.1.0 beta 1 :)


.. code-block:: php

$dispatcher = new Phalcon\Mvc\Dispatcher();
$dispatcher->setModelBinding(true);

return $dispatcher;

It also introduces a new interface :doc:`Phalcon\\Mvc\\Controller\\BindModelInterface <../api/Phalcon_Mvc_Controller_BindModelInterface>` which allows you to define the controllers associated model
to allow model binding in base controllers.

For example, you have a base CrudController which your PostsController extends from. Your CrudController looks something like this:

.. code-block:: php

use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Model;

class CrudController extends Controller
{
/**
* Show action
*
* @param Model $model
*/
public function showAction(Model $model)
{
$this->view->model = $model;
}
}

In your PostsController you need to define which model the controller is associated with. This is done by implementing the :doc:`Phalcon\\Mvc\\Controller\\BindModelInterface <../api/Phalcon_Mvc_Controller_BindModelInterface>`
which will add the getModelName() method from which you can return the model name.

.. code-block:: php

use Phalcon\Mvc\Controller\BindModelInterface;
use Models\Posts;

class PostsController extends CrudController implements BindModelInterface
{
static function getModelName()
{
return Posts::class;
}
}

By declaring the model associated with the PostsController the dispatcher can check the controller for the getModelName() method before passing
the defined model into the parent show action.

If your project structure does not use any parent controller you can of course still bind the model directly into the controller action:

.. code-block:: php

use Phalcon\Mvc\Controller;
use Models\Posts;

class PostsController extends Controller
{
/**
* Shows posts
*
* @param Posts $post
*/
public function showAction(Posts $post)
{
$this->view->post = $post;
}
}

.. highlights::

Currently the dispatchers internal model binding will only use the models primary key to perform a findFirst() on.
An example route for the above would be /posts/show/{1}


Handling Not-Found Exceptions
-----------------------------
Using the :doc:`EventsManager <events>` it's possible to insert a hook point before the dispatcher throws an exception when the controller/action combination wasn't found:
Expand Down