Destramic Posted February 21, 2011 Share Posted February 21, 2011 im tying to design and make a routing system for my mvc framework but im wondering the best way to do this if anyone could help on how the design pattern would be. im thinking i would have a route config file which would contain all the routes for each file routes.config <?php $routes = array('news' => 'news/index'); ?> then the routing class which would get the correct route depeding on the request class and get the dispatch class to load the controller and action? if anyone could help on the best way to do this that would be great...thanks Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/ Share on other sites More sharing options...
ChemicalBliss Posted February 21, 2011 Share Posted February 21, 2011 That could work fine, Though it all depends on your projects structure. This is something you would design into your application, depending on how abtract you want your components. (I'm guessing you've read a little about OOP and MVC structures). You could have it in config as you suggested/In the page controller class itself/in a database such as mysql for dynamic pages. It is entirely your choice depending on how flexible you want your framework to be. hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1177893 Share on other sites More sharing options...
Destramic Posted February 21, 2011 Author Share Posted February 21, 2011 i like the idea of adding the route in the page controller or even in the view page itself $route->add_route('news/test'); but the problem there is that the front controller would load the page controller, method and display the page before you could compare route with url Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1177920 Share on other sites More sharing options...
ChemicalBliss Posted February 21, 2011 Share Posted February 21, 2011 The controller object should have an interface to the route object, though, IMO an extension object of an abstract controller object should be all that is needed to implement a static controller, and have enough flexibility to exchange it for a dynamic controller extension object in the future. Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1177922 Share on other sites More sharing options...
Destramic Posted February 21, 2011 Author Share Posted February 21, 2011 ok and the $route->add_route would be excited in the view template page (with the html) or the page controller?...thanks for you help Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1177928 Share on other sites More sharing options...
ChemicalBliss Posted February 21, 2011 Share Posted February 21, 2011 I would use an extended class that would basically create the functionality of a static controller. The interface should already be defined by its parent abstract class. For example, you could have a controller object for web requests, which would have every method needed for extracting the input data from the client request. Then another for remote commands, by shell for ex, which would hold everything needed for getting arguments passed via a command line. You could even further extend the web request controller to add certain Hard Coded (independent of mod-rewrite) "friendly URL" formats. hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1177959 Share on other sites More sharing options...
Destramic Posted February 23, 2011 Author Share Posted February 23, 2011 well here is my font controller im working on: <?php class Front_Controller { protected $_controller; protected $_request; protected $_route; protected $_inflect; public function __construct() { $this->_request = new Request; $this->_route = new Router($this); $this->_inflect = new Inflection; $this->_request->get_request(); $controller_name = $this->_request->get_controller_name(); $controller = ucfirst($controller_name) . '_Controller'; $model = '_' . $controller_name; $this->_controller = new $controller; $this->$model = new Model($this); } } ?> and this is where i would make sure the route is correct dispatch the page controller and load the action but im still a bit unclear where i would add a route (in the page controller)? Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1178817 Share on other sites More sharing options...
ChemicalBliss Posted February 23, 2011 Share Posted February 23, 2011 $this->_controller = new $controller; Would imo be $this->_controller = new $controller($this->_route); Because in PHP, objects are by default, passed by reference, any object manipulation from a sub class will reflect the class that was passed from, and that goes bettween sub-classes also. So all your classes and subclasses can technically "juggle" the same class, in this case: _route. In your Controller object your constructor would be similar to: <?php class Sometest_Controller { private $_route; public function __construct(Router $_route_object){ $this->_route = $_route_object; // ... } } hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1178836 Share on other sites More sharing options...
Destramic Posted February 25, 2011 Author Share Posted February 25, 2011 ok i see and would i add the route in the method (page)? <?php class Sometest_Controller { private $_route; public function __construct(Router $_route_object) { $this->_route = $_route_object; } protected function news_atricles() { // so url will look like domain.co.uk/news/articles $this->_route->add_route('news/articles'); } Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179613 Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 Yes each page the controller (or some ancillary page control object) loads will be passed to that page object/page (depending how you code your pages). To me this is the most efficient way of passing instantiated objects between each other in PHP 5 - And imo more secure (either from clients or developers) than instantiating objects every use or using global objects. hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179661 Share on other sites More sharing options...
Destramic Posted February 25, 2011 Author Share Posted February 25, 2011 what about if i just set my attributes in my class to public so i can just get the route instance from there? <php class Front_Controller { public $_route; public function __construct() { $this->_route = new Router; ...load controller etc } } class Page_Controller { public function new_articles() { $this->_route->add_route("route here"); } } would that be good or considered as bad practice Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179727 Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 (Guessing that page extends front) Well, it gives rise to a "risk" of bad code, for instance; A Developer creates a main class "String_Sort". (This will sort one string, the string will be treated as an object itself) There are 2 child classes "Display" and "Sort". There is an external class required by "Sort" called "String". Display doesn't need to use the string object, and shouldn't as it has one task: to display an object. Imagine that String is the object of a simple string. String_Sort is an object that will actually reverse the string. Display will Display a String. Sort will reverse an object's string. Flow: String_Sort(String){ protected String; // or even protected, same results/implications) Sort() [extends String_Sort]{ parent->String->reverse(parent->String); // No return needed, the string is modified directly } Display() [extends String_Sort]{ parent->String->reverse(parent->String); // Problem, reversed twice, developer may not know about the above reverse code, he knows though that he has access directly to the string, he also knows the original string needs to be reversed. So he reverses, none the wiser that another developer has already implemented the reverse. echo(parent->String); } } This is the problem of protected, not to mention public objects within objects. With a team of web programmers you want to give them specific guidelines on which to code and how to interact with other objects. This way there is uniformity and it can be expressed much more easily with graphs and (other people) finding and fixing bugs. The object is goign to be a dependancy of the child class that uses it no matter what, passing it via the instantiation not only abstracts the name of the parent property but also prevents other developers from mis-using certain objects. I hope this makes sense (and helps) Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179755 Share on other sites More sharing options...
Destramic Posted February 25, 2011 Author Share Posted February 25, 2011 thank you for your help and time you've really helped alot...i understand how the methd i explained wouldnt be so good...and i'll start to use private instead of protected....but when i comes to me using my View class and when i call to set_variables() in my Page_Controller i'd have to send the view object across in the constuctor like the routing object as you've exlained?...and that would be the best way?...thanks again Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179762 Share on other sites More sharing options...
Destramic Posted February 25, 2011 Author Share Posted February 25, 2011 or of course i could write a wrapper in my Front_Controller <?php class Front_Controller { public function add_route($route) { $this->_route->add_route($route) } } and do the same with the $this->_view->set_variable (create a wrapper) ? Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179764 Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 The View should be abstracted from the code. The Front controller by the looks of it will be your "runtime", So dependancies in there will be the entire website/object base your using. Passing Every Object to it from your Load class (instantiates/sets up the objects for use) should be fine as it is your base/runtime code. From there you would use the passed view class by getting the result object (which would have an interface to keep things easy to maintain/use) from the page and passing that to the view class. Which would then interact with the Page object passed to it using the interface methods alone, create its templates etc and display etc. Quote Link to comment https://forums.phpfreaks.com/topic/228410-creating-a-routing-system-mvc-framework/#findComment-1179767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.