0

In Node we can get an url address with a structure like this /example/:page/:id and we can take the page and id params. Is there a possibility to do something similar using PHP? Or is it only possible using the "?" with all the wanted params after the interrogation point?

I searched for a while and I tried some configurations in the htaccess file. All of them gave some kind of error like 403, 404 or in one of the configurations the intentioned page was loaded but it didn't find the css, js and images files.

Thanks

Edit: I will put the solution I found here because maybe it can be useful for someone someday. After looking for some routers packages, I saw them instructing to put these lines in the htaccess file:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

I've tried something like this before and it was the one that I mentioned in the question that loaded the page but it didn't find the files like css, js, etc.

So I've decide to check the base url and I saw this was the point where the error was coming. After I changed it, the page loaded as the expected and now it's possible to get the value where the users can put a number and redirect to the page that they want (it's something like a magazine).

  • You can get this functionality in the routing component of most modern frameworks. Laravel, symfony, slim, etc. If you want this functionality without a framework, can find a composer package for it, or try to reinvent the wheel yourself. Something like `preg_match` would be how to achieve this yourself, then after doing the match, if it matches your pattern, then you'd have the matching parts that you can grab from the result. Then you'd want to refer to `$_SERVER['REQUEST_URI']` in order to have your starting url to work on after routing to e.g. index.php from your htaccess (assuming apache) – Ultimater May 26 '20 at 03:54
  • Thanks for your answer @Ultimater. The intention wasn't to use a framework even because all the project is already developed without one. It will be only to add a function. I wasn't remembering the name of the function and maybe will be easier to verify if there's a composer package able to do it. – Robério Matos May 26 '20 at 15:14

3 Answers3

0

You can achieve it many ways.

In Laravel (see Documentation). I think every framework now has routing implemented.

Route::get('example/{page}/{id}', function ($page, $id) {
    //
})->where(['page' => '[0-9]+', 'id' => '[a-z]+']);

With Mod-rewrite and then with access through $_GET parameters.

RewriteEngine on
RewriteRule ^example/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=$1&id=$2 [NC]

You can also redirect everything to index.php and there implement your own router. See: Redirect all to index.php using htaccess

RewriteEngine on
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Jsowa
  • 9,104
  • 5
  • 56
  • 60
  • Thanks for your answer @Tajni, I forgot to mention when I asked the question that the project is not using any PHP framework. Look like searching for some package to do this route process is the best solution considerating I don't want to spend lots of time with this thing. – Robério Matos May 26 '20 at 18:01
  • That will be best way. Implementing manually is unnecessary. When you find answer that is most useful on SO, remember you can accept or upvote. – Jsowa May 31 '20 at 11:43
0

Something like this could work

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );

// all of our endpoints start with /person
// everything else results in a 404 Not Found
if ($uri[1] !== 'page') {
    header("HTTP/1.1 404 Not Found");
    exit();
}

For more reference visit this url https://developer.okta.com/blog/2019/03/08/simple-rest-api-php

aatish
  • 57
  • 3
-1

have you tried parse_url()? it will return and associative array which has all the components in your URL

zimorok
  • 326
  • 1
  • 11