Url Routing Basics Tutorial PHP

21,821 views
21,541 views

Published on

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

Published in: Technology

Url Routing Basics Tutorial PHP

  1. 1. URL Routing Basics
  2. 2. OverviewObjectiveLearn the basics setting up routing in your application withProdigyView. This tutorial simulates an MVC.Requirements Understanding of an url Installed version of ProdigyView with a database An understanding of MVC DesignEstimated Time15 minutes www.prodigyview.com
  3. 3. Follow Along With Code Example1. 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
  4. 4. What is RoutingRouting is process of selecting a path to send traffic too. InPHP, url routing is the process of directing traffic to a locationon a website.Most frameworks and CMS using a front end controller tocontrol the process of routing. In this tuturial, we are going touse Heliums routing example.The first thing we have to do is make sure your server isconfigured to handle routing. www.prodigyview.com
  5. 5. Routing - ApacheDepending on your server set up, you can achieve routingin apache by modifying the .htaccess file in site’s rootlocation. For this example, copy the code below into your.htaccess file and the rewrite rule will work.
  6. 6. Routing - NginxIf you are on a Nginx Server, routing is handled a littledifferently. In your sites-available, modify the location sectionof your sites configuration. Add this to the location for routing
  7. 7. ExamplesIn ProdigyView, routing is governed by rules. Refer tohelium’s config/bootstrap/router.php for an example ofthe rule. Each rule below can correspond to a differentaction.Remember these rules, they will be used throughout this tutorial www.prodigyview.com
  8. 8. RulesIn routing, a rule is when a certain uri matches a rules. The rules can beexact or general match. Examples are below. Exact MatchRule – ‘/user/login’Will react only if the uri is ‘/user/login’ Variable MatchRule – ‘/: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
  9. 9. Uri and RulesExamples of uri that will match certain rules are below.
  10. 10. Rules and Your ApplicationOnce a rule is found, is up to your application to handlethe how the rule influences the execution of your code.The rules in this tutorial were designed for an mvc, butthey can change to fit your application. Our next examplewill show helium uses the rule. www.prodigyview.com
  11. 11. 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..3. Set the default value for the 4. Call the controller based upon the variablescontroller and action above.
  12. 12. Get Route VariableTaking a closer look at Heliums router, we can see that thePVRouter::getRouteVariable() method is retrieving a variable.If the variable is defined in one of our rules and is in the uri, itwill be retrieved.In this example, if the uri is /posts/view, it matches one of ourrules. Therefore we can retrieve the variable by calling the part ofthe rule and assign it to our action in our mvc. www.prodigyview.com
  13. 13. Get Route Variable In ControllerWe have a way of retrieving our controller and action inour mvc. But there will be times we need to retrieve othervariables in our application such as an id. Again, we canuse Helium as our example. Proceed to Helium’s postcontroller. www.prodigyview.com
  14. 14. 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 itfind data and pass it too our view.
  15. 15. Challenge!To help better your understanding of routing, complete thisoptional 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.
  16. 16. Review1. Define route rules with the method PVRouter::addRouteRule()2. Configure your web server (Apache or Nginx) to handle rewrite rules3. Using a string form such as /:controller to define variable routes.4. Use PVRouter::getRouteVariable() to retrieve variables from a route. www.prodigyview.com
  17. 17. API ReferenceFor a better understanding of the PVRouter, check out theapi at the two links below.PVRouter More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com

×