Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomated url request with php
    text
    copied!<p>Okay so I have been looking at developing my own custom CMS system for a specific market and I have been looking through a few frameworks and the problem that I have with them is that they don't automate routing. In laravel, if I remember correctly to respond to a url like this you would do something like this:</p> <pre><code>Route::get('/user', function() { return "This is a user"; }); </code></pre> <p>This would essentially listen for a specific request. Now my idea to simplify this was to create an automated router. So what I did was setup an .htaccess file which took every request and directed it to index.php. It also took anything after the .com, so something like <code>www.testsite.com/admin/pages/edit/5</code> and appended it as a get variable.</p> <p>So in the example above I am passing four parameters of a single request:</p> <pre><code>admin - request path / used to signify a login check must be done before passing them on to their request pages - This would be the class or object edit - This would be the method called from the class / object 5 - This would be the actual row of the record in the database being edited </code></pre> <p>So I developed a router class which looks something like this:</p> <pre><code>class router { public $url; public $protectedPaths = array('admin','users','client'); public function __construct() { $this -&gt; url = explode('/', $_GET['url']); if($this -&gt; url[0] == '') { $this -&gt; loadDefaultView(); } else { // Check to ensure that the path is not protected if(in_array($this -&gt; url[0], $this -&gt; protectedPaths)) { // check to ensure user is logged in if($_COOKIE['isLogged']) { // This means that there is no action or model needed just a returned view if($this -&gt; url[2] == '') { $this -&gt; loadViewWithoutAction(); } else { // we check to ensure there is a controller if(file_exists(baseControllers .'controller.'. $this -&gt; url[1] .'.php')) { // require that controller and instantiate it require baseControllers .'controller.'. $this -&gt; url[1] .'.php'; $obj = new $this -&gt; url[1]; // check to see if method exists if(method_exists($obj, $this -&gt; url[2])) { if($_POST) { $data = $_POST; } else { $data = array($this -&gt; url[3]); } // run method if necessary $data = call_user_func_array(array($obj, $this -&gt; url[2]), $data); $this -&gt; loadAdminView( $data ); } else { $this -&gt; loadErrorView(); } } else { $this -&gt; loadErrorView(); } } } else { header("Location: /auth/form"); } } else { // we check to ensure there is a controller if(file_exists(baseControllers .'controller.'. $this -&gt; url[0] .'.php')) { // require that controller and instantiate it require baseControllers .'controller.'. $this -&gt; url[0] .'.php'; $obj = new $this -&gt; url[0]; // check to see if method exists if(method_exists($obj, $this -&gt; url[1])) { // run method if necessary $data = call_user_func_array(array($obj, $this -&gt; url[1]), array($this -&gt; url[2])); $this -&gt; loadPublicView( $data ); } else { $this -&gt; loadErrorView(); } } else { $this -&gt; loadErrorView(); } } } } </code></pre> <p>So I would use a number of different if else statements and perhaps a switch to differentiate between different requests etc. Finally my question, is this bad practice to auto load a class and run a method. From what I've seen in the frameworks, this is all manual and I'm no expert so I'm assuming there is probably a reason for that. In addition, I found little to anything about automating PHP requests in OOP on the web.</p> <p>I'd really like to automate this but at the same time I also do not want to cause security concerns. Oh and for any forms or personal information being input by users everything would be post or ajax to protect against hacking the url. </p> <p>Thanks for any advice or answers in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload