PHP Development Framework kohana3 Custom Routing Settings Example

  • 2021-07-09 07:30:20
  • OfStack

Because there are few users of kohana framework in China, Moreover, the new version is too different from kohana2.X, and the information of kohana3 is mostly in English. Many students who are interested in kohana and want to learn can only sigh at K. Because the company recently turned to kohana3 development (kohana3.1. 0 stable version), I took this opportunity to carefully read the official information. I have benefited a lot, and I will share it with you through my personal station. Today, I will talk about the routing settings of kohana.
Again, I'm using ko 3.1. 0 that differs from ko 3.
Actually, the route setting of kohana3 is very simple. Open bootstrap. php under application file, find Route:: set, and you will see the following default route:

Route::set('default', '((/(/)))')
->defaults(array(
'controller' => 'welcome',
'action'     => 'index',
));

This is the default route, you can see that its composition is like this. Name, controller, action, parameters. In particular, each route must specify the default control and action, 1 is generally index.

How to create a custom route, in fact and the default writing is 1, just add their own things to add. For example, there is a product list page, you need to get the product type ID and the current page number.
Routing can be set as follows


Route::set('product', 'product/((/)(/))')
->defaults(array(
'controller' => 'product',
'action'     => 'index',
'id'             =>0,
'page'       =>0// Some examples here are NULL, But I used an error report .
));

Here, the first product is the name, followed by the key points, product is the controller,/action is the action, 1 must be written like this. Behind (/ < > ) inside is the parameter. Get the parameter here in the page like this, $id = $this- > request- > param ('id'), the name of id in this must be the same as the name of id in the route.
Need students can refer to this example to change, should be able to. Not, strongly recommend you to see the things of 2 stations

1. http://kohanaframework.org/3. 1/guide (official online document)
2. http://kerkness.ca/wiki/doku. php (Unofficial Wikipedia, better examples than official ones, but version 3.0)

Can be compared, I believe everyone 1 can play kohana, come on! !


Related articles: