{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"View","type":"basic","slug":"mvc-view","excerpt":"","body":"# View\n\nMVC can be metaphorically related to a TV. You have various channels, with different information on them supplied by your cable provider (the model). **The TV screen displays these channels to you (the view)**. You pressing the buttons on the remote controls affects what you see and how you see it (the controller). -<a href=\"http://stackoverflow.com/questions/2626803/mvc-model-view-controller-can-it-be-explained-in-simple-terms#answer-2626813\">Tilo Mitra</a>\n\n## Index:\n- [Controller](#controller)\n- [Module](#module)\n- [Learn More](#learn-more)\n\n---\n\n<a name=\"controller\"></a>\n# Controller\n\nWe have a controller actions but wondering how to call our templates.\n\nYou have two ways to do that, there is the facade class ``View`` or you may call the function ``view(...)`` \n```php\n<?php\nnamespace ...;\n\nuse View;\n\nclass MyController extends Controller\n{\n    public function index()\n    {\n        return view('parent_folder.child_folder.file');\n\n        // return View::make('parent_folder.child_folder.file');\n    }\n\n    public function users()\n    {\n        $users = [];\n        # some codes here that calls our model\n        # and probably passing a data on $users array.\n\n        return view('users.index')\n            ->with('users', $users);\n\n            # another way to do it\n            // ->withUsers($users);\n    }\n}\n```\n\nThe sample code above shows how to call a template or our volt template.\n\n### index() action\nInstead of using slash, we're using ``dot`` to access folders. We have ``parent_folder.child_folder.file``, it should access this volt file:\n**project-name/resources/view/parent_folder/child_folder/file.volt**\n\n### users() action\nTo pass in a variable inside the volt or any template engine, we can call this chain function ``with('var_name', 1234)``, you could also try the magic method ``withVarName(1234)`` and it will be interpreted as ``$var_name`` and the value will be ``1234``\n\n\n---\n\n\n<a name=\"module\"></a>\n# Module\n\nYou are wondering how could we change the views directory, we can achieve this by calling ``setViewsDir`` under our DI ``view``\n```php\n// app/modules.php\n<?php\n\nreturn [\n    'main' => function (Phalcon\\Di\\FactoryDefault $di) {\n        $di->get('view')->setViewsDir('/going/to/my/separated/folder');\n    },\n];\n```\n\n---\n\n\n<a name=\"learn-more\"></a>\n# Learn More\n\nTo learn more about the whole ``view`` process, you may click these references:\n- <a target=\"_blank\" href=\"https://docs.phalconphp.com/en/latest/reference/views.html\">https://docs.phalconphp.com/en/latest/reference/views.html</a>\n- <a target=\"_blank\" href=\"https://docs.phalconphp.com/en/latest/reference/volt.html\">https://docs.phalconphp.com/en/latest/reference/volt.html</a>","updates":[],"order":2,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56d648e870eba32d006060b2","parentDoc":null,"user":"56c1105874f0b417004baadc","project":"56c111095abfe40d00be875a","__v":21,"category":{"sync":{"isSync":false,"url":""},"pages":["56d6449db939870b00512c63","56d648d270eba32d006060ae","56d648da70eba32d006060b0","56d648e870eba32d006060b2","56e23cde7d54ba0e00463568"],"title":"Modular-MVC","slug":"modular-mvc","order":2,"from_sync":false,"reference":false,"_id":"56c4275048213b1700af6e33","createdAt":"2016-02-17T07:54:56.500Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d","__v":5},"createdAt":"2016-03-02T01:59:04.608Z","githubsync":"","version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"}}
# View MVC can be metaphorically related to a TV. You have various channels, with different information on them supplied by your cable provider (the model). **The TV screen displays these channels to you (the view)**. You pressing the buttons on the remote controls affects what you see and how you see it (the controller). -<a href="http://stackoverflow.com/questions/2626803/mvc-model-view-controller-can-it-be-explained-in-simple-terms#answer-2626813">Tilo Mitra</a> ## Index: - [Controller](#controller) - [Module](#module) - [Learn More](#learn-more) --- <a name="controller"></a> # Controller We have a controller actions but wondering how to call our templates. You have two ways to do that, there is the facade class ``View`` or you may call the function ``view(...)`` ```php <?php namespace ...; use View; class MyController extends Controller { public function index() { return view('parent_folder.child_folder.file'); // return View::make('parent_folder.child_folder.file'); } public function users() { $users = []; # some codes here that calls our model # and probably passing a data on $users array. return view('users.index') ->with('users', $users); # another way to do it // ->withUsers($users); } } ``` The sample code above shows how to call a template or our volt template. ### index() action Instead of using slash, we're using ``dot`` to access folders. We have ``parent_folder.child_folder.file``, it should access this volt file: **project-name/resources/view/parent_folder/child_folder/file.volt** ### users() action To pass in a variable inside the volt or any template engine, we can call this chain function ``with('var_name', 1234)``, you could also try the magic method ``withVarName(1234)`` and it will be interpreted as ``$var_name`` and the value will be ``1234`` --- <a name="module"></a> # Module You are wondering how could we change the views directory, we can achieve this by calling ``setViewsDir`` under our DI ``view`` ```php // app/modules.php <?php return [ 'main' => function (Phalcon\Di\FactoryDefault $di) { $di->get('view')->setViewsDir('/going/to/my/separated/folder'); }, ]; ``` --- <a name="learn-more"></a> # Learn More To learn more about the whole ``view`` process, you may click these references: - <a target="_blank" href="https://docs.phalconphp.com/en/latest/reference/views.html">https://docs.phalconphp.com/en/latest/reference/views.html</a> - <a target="_blank" href="https://docs.phalconphp.com/en/latest/reference/volt.html">https://docs.phalconphp.com/en/latest/reference/volt.html</a>