Dynamic URL

Post Reply
momo
Ssr. Bludit
Posts: 12
Joined: Thu Jul 06, 2017 9:43 pm

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?
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

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.
Can you please post the code for this.

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
momo
Ssr. Bludit
Posts: 12
Joined: Thu Jul 06, 2017 9:43 pm

in Index.php i use that to switch between different templates

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');
            }
        ?>
could be done cleaner but yeah it works fine :)

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>
I get my data from the api i use for my app :)
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Thank you!

I would try with the file .htaccess.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
momo
Ssr. Bludit
Posts: 12
Joined: Thu Jul 06, 2017 9:43 pm

Any suggestion how it could look like so it doesn't break bludit?

isn't there any other way? like a plugin or a hook before processing the url?
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

momo wrote: Tue Oct 27, 2020 10:28 pm
isn't there any other way?
Use an undocumented native bludit's feature which is custom template field in page's options.
Check this thread viewtopic.php?f=6&t=1416
Post Reply