The Complete Guide to Social Engine 4 Module Development Part 3: Routes, Controllers and Views

Marco Enrico
4 min readFeb 7, 2020

If our cars are going to make rounds in our community pages must be created where users can browse, view, create and edit cars. For this we must define routes, controllers and views.

Social Engine’s routing framework uses Zend Framework’s routing component. It’s advisable to review the doc if you are not familiar with it. Routes are defined in the manifest under the key “routes”.

SE4 defines two default routes a route named “default” and a route named “admin_default”. The default route is the same as the default route of ZF. The default route is /:module/:controller/:action/*. The * means any succeeding values are key/value parameters pairs. This means for example that /core/index/index/foo/bar/ will dispatch the index action of the index controller in the core module with the parameter “foo” set to “bar”.

The admin_default route behaves the same way as the default route but url begins with “/admin”. In addition, the dispatched controller is prefixed with “Admin”. For example /admin/foo/bar/baz/ will dispatch the baz action of the AdminBar controller of the foo module (Foo_AdminBarController). So to figure out which files are rendering a particular admin page all you need to do is look at the url. For example, to figure out the files that is generating the page /admin/user/manage we just need to observe the url itself. From the url it involves the user module, the manage controller of the user module and the index action of that controller. So the controller class is User_AdminManageController in the file /application/modules/User/AdminManageController.php and the view script is /application/modules/User/views/scripts/admin-manage/index.tpl.

Note that any admin controller can also be dispatched using the default route. The index action, of the admin manage controller of the use module can be dispatched with /user/admin-manage/index.

Routes have a name and that name is used with in SE4 in a number of ways. There is a url view helper and a url action helper that constructs urls. For both the url view helper and the url action helper you may set the name of a named route to indicate the you intend to use the named route instead of the default route. The assemble method of the router also uses the name of the route the same way. You may use the route name to indicate the route your are constructing. SE4 also defines the htmlLink view helper. The htmlLink view helper is the reason why you will rarely use the url view and action helper. The first parameter is either an array or a string. If it’s a string it will be the value of the href attribute of the anchor element to be constructed. If it’s an array it is used as routing parameters. To indicate the route you want to use simply add a route key. For example $this->htmlLink(array(‘route’ => ‘group_profile’, ‘id’ => 1), ‘a group’) will generate <a href=”/group/1">a group</a>. In addition you may pass an object that defines the method getHref, the return value of this method is used as the href.

Let’s create some routes.

Each key in the routes array serves as the route’s name. We created routes that cater to specific purposes. The route car_general caters to all cars in general. This route is used when users browse available cars, when a user list and manage the cars he own and to create new cars. The route car_specific is used for actions to be done on individual cars. The car_profile route is used to serve a car’s profile page. This is to be used mainly in a car’s getHref method (discussed later). The car_extended route is used for action such as adding photo’s or video’s of cars.

The route key of the manifest array is used as a parameter to the addConfig method of the router. Again, it is best to consult the zend framework documentation if you are not familiar with it.

From the routes we defined it is necessary for us to create the controllers index, car, and profile and the view scripts for their actions.

Public facing controllers (like the ones we created) extends Core_Controller_Action_Standard. Public facing controllers that requires a logged in user extends Core_Controller_Action_User. Admin controllers extends Core_Controller_Action_Admin.

For each controllers we need to create directory under application/modules/Car/views/scripts named after the name of the controller. So we need views/scripts/profile and views/scripts/car. The one for index is automatically generated by the SDK. For each action in each controller create the appropriate view script file. You may leave them empty for now.

With these in place. You should now be able to navigate to the routes we defined in the manifest. Try navigating to /cars/browse. You should see a page with SE4’s header and footer.

Common problems include not found responses. If this happens. Check the name of the controller that is supposed to be dispatched. It should follow the pattern <ModuleName>_<ControllerName>Controller. Also check the file name of the controller it should be application/modules/<ModuleName>/controllers/<ControllerName>Controller.php. Missing view scripts causes exceptions and should be easy to fix.

Originally published in https://social-engine-tutorials.blogsplot.com.

--

--