I am creating a website let’s say of popular actors. If any actor is clicked a modal is opened, but problem with that is modal can’t be bookmarked or shared. So I used query in URL like https://popularactors.com/?actor=1 and then read $_GET[‘actor’] and opened modal for actor with id=1.
But I have seen direct links like https://popularactors.com/actor/1, How does that work? I can’t be creating separate html page for every actor. How is this achieved? I don’t need separate page but URL like this "https://popularactors.com/actor/1" to open modal, like I’m doing by passing query variable.
I’m using XAMPP.
2
Answers
https://popularactors.com/actor/1 is almost certainly rendering its HTML for a given actor via backend code, just like your example. It’s just doing it using a server’s ability to rewrite urls, along with perhaps a PHP framework (or some other server-side language’s framework). If you want to learn more about that, there are lots of places to go. One example is how Symfony does it, often with mod_rewrite.
https://symfony.com/doc/current/routing.html
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
On the other hand, a client-side-only approach might use a hash (#) to separate your server-rendered url from some string that tells a frontend framework like Vue or React what to render. So, for example, you might go to https://popularactors.com/actor#1 in your browser. The server would then send a generic page, and your frontend code would then need to make an ajax call (or maybe show a modal containing prepopulated text, depending on how short the list of actors is) based on that hash value.
If you’re prepopulating all of the actors’ information and just using JS to put a specific actor inside a modal, you also then have the advantage of making a page that is SEO-friendly. If you’re only getting actor information via ajax calls, then that SEO goes out the window, since bots can’t "see" the information. Something to keep in mind.
Anyhow, I hope that helps!
Let’s look at an example below:
You can see that these 3 parts are repeated:
This basically means, get everything. It would recommend look into regex if this is not clear to you.
Everything get redirect to index.php with 3 variables: $module, $action and $id
In index.php you can use these variables to do the logic you want.
Example url: