Dynamic URL
hello is there a way to make a Dynamic URL without the use of classic Query String:
My goal is to make this page:
https://gohafla.com/rec?id=311
work like this:
https://gohafla.com/rec/311
What I do right now is the following, I have set up a page in admin with "rec" Where i define the template for the page and in the template code i have a check if page has template "rec" then display it different and incorporate the query string "id" to get data from my db.
Any suggestions on how to make it look cleaner and better?
My goal is to make this page:
https://gohafla.com/rec?id=311
work like this:
https://gohafla.com/rec/311
What I do right now is the following, I have set up a page in admin with "rec" Where i define the template for the page and in the template code i have a check if page has template "rec" then display it different and incorporate the query string "id" to get data from my db.
Any suggestions on how to make it look cleaner and better?
- Edi
- Site Admin
- Posts: 3121
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 54 times
- Been thanked: 77 times
- Contact:
Can you please post the code for this.momo wrote: ↑Sun Oct 25, 2020 6:56 pm What I do right now is the following, I have set up a page in admin with "rec" Where i define the template for the page and in the template code i have a check if page has template "rec" then display it different and incorporate the query string "id" to get data from my db.
Rewrite of URLs can be done in the file .htaccess. But the problem could be the "/" because it means normally a directory.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
in Index.php i use that to switch between different templates
could be done cleaner but yeah it works fine
and in rec i use that:
I get my data from the api i use for my app
Code: Select all
<?php
if ($WHERE_AM_I == 'page') {
switch ($page->template()) {
case 'rec':
include(THEME_DIR.'rec.php');
break;
case 'homepage':
include(THEME_DIR.'home.php');
break;
default:
include(THEME_DIR.'page.php');
break;
}
} else {
include(THEME_DIR.'page.php');
}
?>
and in rec i use that:
Code: Select all
<?php $json = file_get_contents('https://api.gohafla.com/v1/getRec?key=[...]&id='.$_GET["id"]);
$result = json_decode($json);
$pages->edit(array(
'key'=>$page->key(),
'title'=> $result->title,
'description'=> substr($result->description, 0, 100),
));
?>
<meta property="og:title" content="<?php echo $result->title ?>">
<meta property="og:description" content="<?php echo substr($result->description, 0, 100) ?>">
<meta property="og:image" content="<?php echo $result->images[0]->source ?>">
<div class="">
<div class="card rec-card" style="max-width: 480px; margin: auto;">
<img class="card-img-top" id="rec-image" src="<?php echo $result->images[0]->source ?>" alt="<?php echo $result->title ?>" height="580" style="object-fit: cover;">
<div class="card-body">
<h5 class="card-title" id="rec-title"><?php echo $result->title ?></h5>
<p id="rec-description"><?php echo substr($result->description, 0, 100); ?> ...</p>
</div>
</div>
</div>