Home Rewrite url with 2 or more params

# Rewrite url with 2 or more params

emma
1#
emma Published in 2017-12-05 21:14:59Z
 I asked a question like this before but since i still can't find an answer to this i'll ask it again :-s. I'm using this very basic 'templating' script: require_once 'core/init.php'; if(empty($_GET['page'])){ header('Location: home'); die(); }$basePath = dirname(__FILE__) . '/'; $viewPath =$basePath . 'view/'; $view = scandir($viewPath); foreach($view as$file) { if (!is_dir($viewPath .$file)) { $pages[] = substr($file, 0, strpos($file, '.')); } } if(in_array($_GET['page'], $pages)){ include($viewPath . $_GET['page'] . '.php'); } else{ include($basePath . '404.php'); }  and i'm rewriting my url from /base/index.php?page=somepage to /base/somepage(somepage is a .php file in my template folder) using this htaccess file RewriteEngine On RewriteBase /base/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^?]*) index.php?page=\$1 [L,QSA]  With 1 parameter it works just fine but my problem is that i don't know how to rewrite a second param /base/profile?user=username (with no htaccess file this would have look like this /base/index.php?page=profile?user=username) and i want it to look like this /base/profile/username. I hope that this question is understandable :-s
Zyigh
2#
 Routing is a real issue and I can't be exhaustive in one comment, but I'll try to do my best. Please forgive my aproximative english and let me know if you don't understand. I still have a lot to learn so I'll try to explain through something I made, but it is probably fully improvable. Today's PHP Standard Recommendation about routing and interpreting request should implement PSR7. I personnaly use it through a FrontController Design Pattern in a MVC framework I'm building to understand these concepts. My folders are organized like this : Public : Where I launch my web server, where you can find JavaScript/CSS. There's an index.php which just contains require_once('../index.php'); App : Where there's the router and mostly all generic code Src : Where there's the specific code to the app. That means controllers and entities for now. Vendor : Composer dependencies such as GuzzleHTTP to have a class between the actual request and the code. Here's the code in my root's index.php : getResponse());  The main point of it is that I interpret the request within an instance of a class implementing PSR7. In my FrontController, my request travel through some methods (such as removing trailing slash) to finnaly be sent in a Router class to be handled. The purpouse of my Router class is to check if the URI exist in the array where I stocked all my routes under this format in a json file : { "/": [ "AppController", "indexAction", ["GET", "POST"] ],... }  This is where I use regex to match variable inside the URI (/article/:id for example) too. This Class can be resumed as "Does this URI exists in my app?". At this point, I instantiate a new Route class with all the array as parameter. From here, I have to answer questions such as "Is it attached to a method in a controller ?", "Does the method in which it is asked is handled ?" ... To summarize, at this point, I have an Instance of a Class that represents the Request, another one that represents all my routes. I confront them to get ONE Route which I'm gonna manipulate through an Instance of a Class Route. If it passed all those tests, then I can instantiate the right Controller, where there will be the logical part specific of the app, requiring some action to get data, that I will send in my views to generate a HTML output which I will send back all the way back to my function send so the output is displayed when you ask for a specific URI. The main point of this long answer is to show you something that is almost completely independent from the server. It's also useful if your app gets bigger and has to handle more specific rules for routing. It forces you to separates all the bundles of your app : A Controller is not a Model neither a Router... Try to find some good tutorials to learn Oriented Object Programmation in PHP, which would avoid easy security issues and give you much more comfort when developping an app :) Hope it was understandable and helpful