17
URL Routing Basics

Url Routing Basics Tutorial PHP

Embed Size (px)

DESCRIPTION

Learn how to route url's and uri's in ProdigyView. Also learn how to configure the server's rewrite rules to handle route.

Citation preview

Page 1: Url Routing Basics Tutorial PHP

URL Routing Basics

Page 2: Url Routing Basics Tutorial PHP

OverviewObjective

Learn the basics setting up routing in your application with ProdigyView. This tutorial simulates an MVC.

Requirements

Understanding of an url

Installed version of ProdigyView with a database

An understanding of MVC Design

Estimated Time

15 minuteswww.prodigyview.com

Page 3: Url Routing Basics Tutorial PHP

Follow Along With Code Example

1. Download a copy of the example code at www.prodigyview.com/source.

2.Install the system in an environment you feel comfortable testing in.

3.Proceed to mvc/helium/config/bootstrap/router.php

www.prodigyview.com

Page 4: Url Routing Basics Tutorial PHP

What is RoutingRouting is process of selecting a path to send traffic too. In PHP, url routing is the process of directing traffic to a location on a website.

Most frameworks and CMS using a front end controller to control the process of routing. In this tuturial, we are going to use Helium's routing example.

The first thing we have to do is make sure your server is configured to handle routing.

www.prodigyview.com

Page 5: Url Routing Basics Tutorial PHP

Routing - Apache

Depending on your server set up, you can achieve routing in apache by modifying the .htaccess file in site’s root location. For this example, copy the code below into your .htaccess file and the rewrite rule will work.

Page 6: Url Routing Basics Tutorial PHP

Routing - NginxIf you are on a Nginx Server, routing is handled a little differently. In your sites-available, modify the location section of your sites configuration.

Add this to the location for routing

Page 7: Url Routing Basics Tutorial PHP

ExamplesIn ProdigyView, routing is governed by rules. Refer to helium’s config/bootstrap/router.php for an example of the rule. Each rule below can correspond to a different action.

www.prodigyview.com

Remember these rules, they will be used throughout this tutorial

Page 8: Url Routing Basics Tutorial PHP

RulesIn routing, a rule is when a certain uri matches a rules. The rules can be exact or general match. Examples are below.

Exact Match

Rule – ‘/user/login’

Will react only if the uri is ‘/user/login’

Variable Match

Rule – ‘/:controller/:action’

Will react to a uri that has two elements. This mean the uri such as

‘/something/random’ will have a match.

These uri will not match with the rule aboe.

‘/something/random/more’ or ‘/something’ .

Using ‘/:variable_name’ will make the rule match any variable in the uri

Page 9: Url Routing Basics Tutorial PHP

Uri and RulesExamples of uri that will match certain rules are below.

Page 10: Url Routing Basics Tutorial PHP

Rules and Your ApplicationOnce a rule is found, is up to your application to handle the how the rule influences the execution of your code.

The rules in this tutorial were designed for an mvc, but they can change to fit your application. Our next example will show helium uses the rule.

www.prodigyview.com

Page 11: Url Routing Basics Tutorial PHP

Use the rules in application

Below is Helium’s MVC and how it routes based on the rules.

1. Get the route 2. If the route’s controller and action is not set, use the variables form the uri. Otherwise, use variables in route..

4. Call the controller based upon the variables above.

3. Set the default value for the controller and action

Page 12: Url Routing Basics Tutorial PHP

Get Route VariableTaking a closer look at Helium's router, we can see that the PVRouter::getRouteVariable() method is retrieving a variable. If the variable is defined in one of our rules and is in the uri, it will be retrieved.

In this example, if the uri is /posts/view, it matches one of our rules. Therefore we can retrieve the variable by calling the part of the rule and assign it to our action in our mvc.

www.prodigyview.com

Page 13: Url Routing Basics Tutorial PHP

Get Route Variable In Controller

We have a way of retrieving our controller and action in our mvc. But there will be times we need to retrieve other variables in our application such as an id. Again, we can use Helium as our example. Proceed to Helium’s post controller.

www.prodigyview.com

Page 14: Url Routing Basics Tutorial PHP

Inside the ControllerLook at the beginning of our view method in the the controller. From the rules we set before, we can retrieve the ID and use it find data and pass it too our view.

Page 15: Url Routing Basics Tutorial PHP

Challenge!

To help better your understanding of routing, complete this optional challenge.

1. Create a front end controller has multiple actions to execute.

2. In your router, add route route rules.

3. In your front end controller, set up a mapping system that executes commands depending on if the rule matches.

4. Run your front end controller and ensure that the correct commands are executed based on the route passed.

Page 16: Url Routing Basics Tutorial PHP

Review1. Define route rules with the method

PVRouter::addRouteRule()

2. Configure your web server (Apache or Nginx) to handle rewrite rules

3. Using a string form such as /:controller to define variable routes.

4. Use PVRouter::getRouteVariable() to retrieve variables from a route.

www.prodigyview.com

Page 17: Url Routing Basics Tutorial PHP

API ReferenceFor a better understanding of the PVRouter, check out the api at the two links below.

PVRouter

www.prodigyview.com

More Tutorials

For more tutorials, please visit:

http://www.prodigyview.com/tutorials