Updated 051 Dynamic Router Implementation Explained (markdown)

Amigo 2019-09-04 08:56:49 +02:00
parent ebf000af5c
commit 19cc29a106
1 changed files with 7 additions and 12 deletions

@ -5,38 +5,33 @@
[00:00:00](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m00s)
(_Click on these time links to see Youtube video_)
I would like to demonstrate the new implementation which is not such a major thing but something we have done to deal with some of the router complexity. If a component is built and has a front end for your component, and you have Site Views. [00:00:17](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m17s) Your Site Views usually are getting its data from a Dynamic Gets which you link up to the Site View. This Dynamic Gets returns a result set and it is from this result set that we should get information by which we combined with the view name, build what is called a search engine friendly URL. <<<<<<<<
I would like to demonstrate the new implementation which is not such a major thing but something we have done to deal with some of the router complexity. If a component is built and has a front end for your component, and you have Site Views. [00:00:17](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m17s) Your Site Views usually are getting its data from a Dynamic Gets which you link up to the Site View. This Dynamic Gets returns a result set and it is from this result set that we should get information by which we combined with the view name, build what is called a search engine friendly URL.
### Search Engine URL Is Done With Router - JCB Builds Router
[00:00:49](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m49s)
Your search engine friendly URL is done with the router. JCB also builds the router and it sort of guesses what should be these values. Let me compile a component and show you what it guessed, and then see how we would need to change it. The component that we're working with is Sermon Distributor. I'm going to compile it, and install it on this website. [00:01:16](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m16s) The router doesn't really work. I'm not going to demonstrate the actual front-end.
Your search engine friendly URL is done with the router. JCB also builds the router and it sort of guesses what should be these values(See video). Let me compile a component and show what it guessed, and then see how we would need to change it. The component that we are working with is Sermon Distributor and it is going to be compiled and installed on this website. [00:01:16](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m16s) The router does not work. I am not going to demonstrate the actual front-end.
### Code - 'com_sermondistributor' - router.php
[00:01:26](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m26s)
I'm just going to show you the code. When you look at the website you've got your root directory 'Joomla mount', 'admin' and then 'components'. In here we've got and there's a file called 'router.php'.
This is explained in the code. In the website there is a root directory 'Joomla mount', 'admin' and then 'components'. In there is Sermon Distributor and there is a file called '`router.php`'.
### parse - switch(segments[ ]) - First Value
[00:01:44](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m44s)
I'm going to open router.php. You'll see there's a function called 'build', a class method as well as a function called 'parse'. In this function called parse, there is a 'switch' which makes decisions based on the 'segment's' first value. [00:02:08](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m08s) Usually that would be the view name in this first value. It determines which view are we looking at, is it, preacher? and so we are going through the list. By default without us making any changes, JCB builds that for us, [00:02:31](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m31s) all of it.
Open` router.php`. There is a function called '`build`', a class method as well as a function called '`parse`'. In this function called `parse`, there is a '`switch`' which makes decisions based on the '`segment's`' first value. [00:02:08](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m08s) Usually that would be the view name in this first value. It determines which view are we looking at, is it, `preacher`? and so we are going through the list. By default without us making any changes, JCB builds that for us.
### preachers - Getting All Items From Database With No Input From URL
### Preachers - Getting All Items From Database With No Input From URL
[00:02:33](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m33s)
If we look at preachers, which is a listview. Let's open the model, preacher.php. Here are preachers, we can scroll down and we'll see that in its [00:02:51](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m51s) query, it has getItems and here it got a getListQuery. It is getting all the items from Database with no input from the URL whatsoever. It doesn't do any of that and then it just gives back. It does not need a URL value. [00:03:18](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m18s) So that means it most probably will only set this value. All of this(see video) will really be redundant, it won't be used, because it's a listview, there isn't an alias. We not looking at an individual item so there isn't an id, we could remove this here(see video), this code [00:03:44](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m44s) it's not doing anything. It is not going to slow your site down that dramatically, it's hardly noticeable. It is a default being generated.
If we look at `preachers`, which is a listview and open the model, `preacher.php`. Here are `preachers`, we can scroll down and see that in its query, it has `getItems` and here it got a `getListQuery`. [00:02:51](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m51s) It is getting all the items from the database with no input from the URL whatsoever. It does not do any of that and then it just gives it back. It does not need a URL value. [00:03:18](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m18s) So that means it most probably will only set this value `$ vars['view']='preachers';`. All of this(follow on video) will be redundant because it is a listview, there is not an alias. We are not looking at an individual item so there is not an `id`, this code can be removed. [00:03:44](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m44s) It is a default being generated.
Whereas if we look at this, for example, preachers is the listview, but then there is a view called preacher, we see it here. It is saying that [00:04:09](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m09s) it needs to get the id from the sermon table. Which is an error. If I open the preacher, and I look at the getlistQuery, then I see that in the getListQuery, it is getting an id from the URL. It is asking that it should be = to preacher. [00:04:34](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m34s) It is the main table, the sermondistributor table, but it's not looking for the sermon id, it is looking for the preacher value in the sermon table. That's why JCB be fell back onto the table name. Yet it should go to the preacher table and see whether [00:04:59](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m59s) that preacher value is = to this id. It is making an error. You wrote the code or you set up the get so you should understand the logic of what you see in the code. If you do not, then this is a feature with JCB has, which you possibly won't be able to make use of. Like we've said many times JCB is for those who know PHP, and can write their own components.
Preachers are the listview, but then there is a view called `preacher`, and it says that it needs to get the `id` from the `sermon` table which is an error. [00:04:09](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m09s)If I open the `preacher`, and look at the `getlistQuery`, there it is getting an `id` from the URL. It is asking that it should be `= to preacher`. [00:04:34](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m34s) It is the main table, `sermondistributor` , but it is not looking for the `sermon id`, it is looking for the `preacher` value in the sermon table. That is why JCB fell back onto the table name. Yet it should go to the preacher table and see whether [00:04:59](https://www.youtube.com/watch?v=hYycPLbaMos&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m59s) that preacher value is `=` to this `id`. It creates an error. You should understand the logic of what you see in the code, if not, then this feature in JCB, will not be useful for you. <<<<<<
### getVar Class - See What It Is Going To Do - How It Is Getting The Values