Slim/Custom routes

From Aimeos documentation

Slim
Other languages:
English 100%


Routes are defined in the src/aimeos-routes.php of your Slim application and a set of standard routes is provided by default. Each route has at least a name, the corresponding path and the call to the controller action:

  1. $app->map(['GET', 'POST'], '/myaccount', function( $request, $response, $args ) {
  2. 	return \Aimeos\Slim\Controller\Account::indexAction( $this, $request, $response, $args );
  3. })->setName( 'aimeos_shop_account' );

Each route creates links to one page of your application. The route names are important for the core library because the Slim router needs their names for generating the correct routes for the given parameter.

Route names

The names of the standard routes are predefined in the ./src/aimeos-routes.php file. Besides the admin routes not listed here, their names are:

  • aimeos_shop_account ("my account" page for each user)
  • aimeos_shop_account_favorite (favorite products in "my account")
  • aimeos_shop_account_watch (watched products in "my account")
  • aimeos_shop_basket (standalone basket page)
  • aimeos_shop_count (product counts for the faceted search in JSON format)
  • aimeos_shop_detail (product detail page)
  • aimeos_shop_list (product list page)
  • aimeos_shop_session_pinned (pinned products in customer session)
  • aimeos_shop_suggest (list of products for search suggestions in JSON format)
  • aimeos_shop_stock (product stock information in JSON format)
  • aimeos_shop_checkout (standalone checkout process page)
  • aimeos_shop_confirm ("thank you" page after completing the order)
  • aimeos_shop_update (payment update notifications)
  • aimeos_shop_terms (terms and conditions page)
  • aimeos_shop_privacy (privacy policy page)

They are split into several route groups: "admin", "account", "default", "confirm" and "update". Each of these route groups can be configured separately in the "routes" array of your ./src/aimeos-routes.php.

Adapt existing routes

For all existing routes, you can change their path and the route parameters by changing the routes in your local ./src/aimeos-routes.php file. For example, to change the path of the "aimeos_shop_terms" to "/terms_and_conditions", you only need to modify the path in the ./src/aimeos-routes.php file:

  1. $app->map(['GET', 'POST'], '/terms_and_conditions', function( $request, $response, $args ) {
  2. 	return 'terms';
  3. })->setName( 'aimeos_shop_terms' );

Similarly, calling your own controller and action ("MyController" and "mytermsAction") implemented in your application is done by adapting function body of the route:

  1. $app->map(['GET', 'POST'], '/terms', function( $request, $response, $args ) {
  2. 	return \Aimeos\Slim\Controller\MyController::mytermsAction( $this, $request, $response, $args );
  3. })->setName( 'aimeos_shop_terms' );

More details can be found in the Slim router documentation.

Routes for multiple site, languages and currencies

Aimeos is able to manage many sites in one installation with different languages and currencies for each site. If you have such a setup with at least two sites, languages or currencies, you need to adapt at least the values of the "account", "default", "confirm" and "update" settings in the "routes" array of your ./src/aimeos-settings.php:

  1. 'routes' => array(
  2. 	'account' => '{site}/{locale}/{currency}',
  3. 	'default' => '{site}/{locale}/{currency}',
  4. 	'confirm' => '{site}/{locale}/{currency}',
  5. 	'update' => '{site}/{locale}/{currency}',
  6. );

The value for the route groups can be not only used to add a fixed prefix string like "/shop" but also be changed to support these placeholders for the route definition:

  • {site} (unique site code from "code" field in the "mshop_locale_site" table)
  • {locale} (language ID from the "langid" field in the "mshop_locale" table)
  • {currency} (currency ID from the "currencyid" field in the "mshop_locale" table)

You can also reorder the placeholders the way you prefer like moving the "{locale}" placeholder to the front. If your shop doesn't use more than one site, language or currency, you can also leave out those placeholders. The Aimeos shop will automatically use the default values in this case. For the "site" parameter it's always "default" and for the language and currency the ones you've defined in the "Locale" tab of the administration interface.

Add new routes

If you've implemented a new Aimeos core component which generates URLs to a new page, you have to add a new custom route. The route name can be freely chosen but for clarity, you should prefix it with "aimeos_shop_", e.g.

  1. $app->map(['GET', 'POST'], '/new_page', function( $request, $response, $args ) {
  2. 	return \Aimeos\Slim\Controller\MyController::myAction( $this, $request, $response, $args );
  3. })->setName( 'aimeos_shop_mypage' );

The Aimeos core component now needs to know the name of the new route. It's then handed over to the Slim which will generate the correct URL using the available parameters. The route name must be configured in the ./src/aimeos-settings.php file, e.g.

  1. 'client' => array(
  2.     'html' => array(
  3.         'mycomponent' => array(
  4.             'mypart' => array(
  5.                 'url' => array(
  6.                     'target' => 'aimeos_shop_mypage',
  7.                 ),
  8.             ),
  9.         ),
  10.     ),
  11. ),

The "mycomponent" and "mypart" names must be replaced by the name of your Aimeos HTML client component and the part you've implemented.