Here’s my question:
I develope a site where users would add content by themselves.
That content gets an identifier by id and/or alias to be extracted from db. The goal is NOT to create views and controllers for EVERY page containing that content. We shouldn’t seek those for them. Instead we handle URL in actionIndex() getting the URL part that defines appropriate content and further load it into template.
For example:
-
We have a main section Products. Its view placed at
/views/Products/index.php
-
We have a controller:
ProductsController extends Controller
-
We have a default method actionIndex()
So when we get URL like this:
/mysite/Products/some_product_alias
…we should handle it in actionIndex() so it wouldn’t direct us to the 404 page but get data by alias “some_product_alias” loading it into products template (I guess it shold be /views/Products/index.php or some another page).
In the same time it needed to keep a possibility to define controller/actionMethod for static pages which are main sections of the site and being the main menu.
That is it should work so:
If it can find out an existing actionMethod in controller it just calls it. If it can not find it, it seeks an alias extracted from URL in DB. If it exists there then it gets data and load it into template.
For sure this task is quite manageable but unfortunately I’m rookie for Yii and still don’t know how to do it 🙁
I will very appreciated for your help!
The answer (its first part) is almost that DeadAlready told.
But here is necessary to add something.
1. We should set a rule for our alias into urlManager (yes!):
note that it will call actionIndex() in controller and won’t look for a method by the alias name.
2. We MUST point a variable $alias (which name is the same that an pattern substring i.e. “$alias”/”
After that we can handle events to get data from DB.