r/PHPhelp • u/cucca77 • Oct 01 '24
index.php route with params
hello everyone,
I'm a newbie and I'm looking for a way to have an index.php that routes to the pages of my personal "site".
At the moment I'm using herd for convenience since it has everything packaged (I don't use laravel, only smarty).
From what I understood from searching on the internet herd already has the rewrite for nginx that redirects requests to index.php, so I just need to write a correct index.php.
From a tutorial I found this implementation:
<?PHP
$request = $_SERVER['REQUEST_URI'];
$viewDir = '/views/';
switch ($request) {
case '':
case '/':
require __DIR__ . $viewDir . 'home.php';
break;
case '/views/users':
require __DIR__ . $viewDir . 'users.php';
break;
case '/contact':
require __DIR__ . $viewDir . 'contact.php';
break;
default:
http_response_code(404);
require __DIR__ . $viewDir . '404.php';
}
?>
the problem is that if I call http://<mysite>.test/views/users?id=1 it always gives me the 404.php page because I pass variables on the url... what is the correct way to manage the routes on the index.php?
(I could simply do a substring up to the ? but it doesn't seem very elegant...)
thanks to everyone
6
u/uncle_jaysus Oct 01 '24
Ahh routing… so many ways to go about it.
Using switch can get a bit unwieldy if too many pages, but for simple site it works... Anyway, to answer your question, you can explode REQUEST_URI by ? to get rid of the querystring.
$request = explode('?',$_SERVER['REQUEST_URI'])[0];
3
1
u/equilni Oct 01 '24
I'm a newbie
I would decide then full query string or rewrite urls. Next, consider the url - do you really need view as part of the URL? Also, consider going plural to singular noting a list vs single record (users vs user/1)
/users?id=1 full query string could be ?controller=user&id=1 - see how I went singular?
Pseudo code could look like:
// ?controller=user
case 'user':
// &action=login
switch ($action) {
case 'login':
if (user is logged in) {
// 404 or already logged in note
}
switch ($requestMethod) {
case 'GET':
# show login form
break;
case 'POST':
# check credentials
break;
}
break;
....
}
// &id=1
switch ($id) {
if (user is NOT logged in) {
// 404
}
switch ($requestMethod) {
case 'GET':
# show user $id
break;
case 'POST':
# process user $id
break;
}
}
break;/users?id=1 full rewrite would be /user/1 - see how I went singular?
You will want to use parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) to get the path, then work from there. if users, get list of users. is user, check for parameter, pass parameter to get the single user
2
u/cucca77 Oct 01 '24
thanx a lot! Yes the full query string could be ?controller=user&id=1
my goal is to have all user requests going through the index... a bit like laravel does (even though I'm not using an MVC because it still messes up my brain :P)
Thanx for you help!2
u/equilni Oct 01 '24
Requests to the index is common. Also this is considered a Front Controller pattern, so you are doing part of MVC.
MVC isn’t a hard concept to grasp.
Route the request and send the final response (Controller)
Process the request and send a response (Model)
Output the response (View)
1
u/hedrumsamongus Oct 01 '24
https://kevinsmith.io/modern-php-without-a-framework/
This was my guidebook when I moved away from standalone page scripts to a modern front-controlled, dependency-injected architecture. I think it's a great tutorial, and it should get you up and running in just a couple hours.
One note: the narrowspark/http-emitter project is dead, but fortunately, due to PSR standards, you can use another emitter without any issues. We switched to the laminas/laminas-httphandlerrunner SapiEmitter, which is actively maintained here.
1
u/ishanvyas22 Oct 01 '24
Just use a routing library 🤷♂️ They have already solved this problem, not a smart idea to reinvent the wheel.
0
u/akkruse Oct 03 '24
I'm on my phone but here are the important parts:
$uri = $_SERVER['REQUEST_URI'];
$queryString = parse_url($uri, PHP_URL_QUERY);
$params = [];
parse_str($queryString, $params); // sets $params
$id = $params['id'];
5
u/MateusAzevedo Oct 01 '24
That would be my solution.
An alternative would be regex and it allows for URLs like
users/[id], but that would be very overkill for your case. Or you can use an existing router library.