Laravel/Custom routes

From Aimeos documentation

Other languages:
English 100%

Routes are defined in the src/routes.php of the Aimeos shop package and a set of standard routes is provided by default. Each route has at least a name, a corresponding path, and a controller@action part:

  1. Route::match( array( 'GET', 'POST' ), '<path of the route>', array(
  2. 	'as' => '<route name>',
  3. 	'uses' => 'Aimeos\Shop\Controller\<controller name>@<action name>'
  4. ));

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

Route names

The names of the standard routes are predefined in the routes.php file. Besides the "admin" routes, which are not listed, their names are:

  • aimeos_shop_account ("my account" page for each user)
  • aimeos_shop_account_download (file downloads for virtual products)
  • 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. Each of these route groups can be configured separately in the "routes" array of your "config/shop.php".

Adapt existing routes

For all existing routes, you can change the path and the default parameters by redefining the routes in your routes/web.php file (Laravel 5.1/5.2: app/Http/routes.php) using the same name. For example, to change the path of the "aimeos_shop_terms" to "/terms_and_conditions", you only need to create a route with that path in the routes.php file:

  1. Route::get('terms_and_conditions', array(
  2. 	'as' => 'aimeos_shop_terms',
  3. 	'uses' => '\Aimeos\Shop\Controller\PageController@termsAction'
  4. ));

Similarly, changing the controller and action name to a custom one ("MyController" and "mytermsAction") implemented in your application is done by adapting the "uses" value in the second parameter of the route:

  1. Route::get('terms', array(
  2. 	'as' => 'aimeos_shop_terms',
  3. 	'uses' => 'MyController@mytermsAction'
  4. ));

In this case, your controller must be placed in the ./app/Http/Controllers directory. More details can be found in the Laravel routing documentation.

Routes for multiple sites, languages and currencies

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

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

The value for the "prefix" key can be used to add a fixed prefix string (like "/shop") and can 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 reorder the placeholders the way you like such as moving the "{locale}" placeholder to the front. If your shop does not use more than one site, language, or currency, you can leave those placeholders out. The Aimeos shop will automatically use the default values in this case. The "site" parameter is always "default" and the language and currency parameters are the ones defined in the "Locale" tab of the administration interface.

You cannot add the "site", "locale", and "currency" placeholders to the "admin" routes of the Aimeos package. At least the "site" placeholder is already part of that route and using the same placeholder twice in one route will fail.

Add new routes

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

  1. Route::get('new_page', array(
  2. 	'as' => 'aimeos_shop_mypage',
  3. 	'uses' => 'MyController@myAction'
  4. ));

The Aimeos core component will need to know the name of the new route. It's then handed over to Laravel, which will generate the correct URL using the available parameters. The route name must be configured in the config/shop.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.