1074

How to load routes dynamically to custom_routes.php

Comments for “How to load routes dynamically to custom_routes.php”
 

Posted by siri on Saturday 18th May 2024 at 06:08 GMT

Hi,
I was trying to load routes dynamically from a db. But that is not working.
function fetch_dynamic_routes() {
    $url = BASE_URL . 'static_pages/getroutes';    
    // Initialize cURL session
    $ch = curl_init();    
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Execute cURL session
    $jsonData = curl_exec($ch);    
    // Close cURL session
    curl_close($ch);    
    return $jsonData;
}
$dynamicRoutes = fetch_dynamic_routes();
print_r($dynamicRoutes);

define('CUSTOM_ROUTES', $routes);

When I tried this , my web page is taking ages to load and getting below warning
Warning: file_get_contents(http://localhost/dake/static_pages/getroutes): Failed to open stream: HTTP request failed! in E:\xampp8.2.4\htdocs\dake\config\custom_routing.php on line 7

But everything is fine in php.ini file. So how to load routes dynamically ?
Thanks
Siri K
Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by siri on Friday 24th May 2024 at 07:37 GMT

Hi Simon,
Can you please look into this?
Thanks
Siri K
Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

Posted by DaFa on Friday 24th May 2024 at 10:50 GMT

Gee Siri, I looked at this post a few days ago and it was making my head hurt 🥴

You haven't given me much to go on - except a lot of me assuming what the f@rk is he trying to do here...

1. Assuming you are putting fetch_dynamic_routes() in custom_routing.php, just before it defines 'CUSTOM_ROUTES' replacing the $routes array?
• if so, it seems you're creating a loop with your cURL as you're calling an endpoint within your domain/app using $url = BASE_URL . 'static_pages/getroutes';. This might cause cURL to wait indefinitely until it times out.

2. It's crucial to understand the order in which things are loaded with Trongate.
• hint - follow the logic flow of index.php in the public folder

OK, now we have a better understanding of when things are loaded, I can now try and guess what you want.

3. I'm assuming you have created a module that creates dynamic routes?
• assuming your table looks like this
CREATE TABLE dynamic_routes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    route VARCHAR(255) NOT NULL,
    destination VARCHAR(255) NOT NULL
);

• and you have the standard routes already in there
INSERT INTO dynamic_routes (route, destination) VALUES
('tg-admin', 'trongate_administrators/login'),
('tg-admin/submit_login', 'trongate_administrators/submit_login'),
('trongate-pages', 'trongate_pages/index');

4. Let's now create a file in public and call it dr.php
(the help bar won't let me paste in a require_once for ../ config / database.php so add it below)
<?php
require_once 'xxxxxxxxxxxxx';

header('Content-Type: application/json');

$host = HOST;
$db = DATABASE;
$port = '';
$user = USER;
$pass = PASSWORD;
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;port=$port;dbname=$db;charset=$charset";

$options = [
  PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
  $pdo = new PDO($dsn, $user, $pass, $options);

  // SQL query to fetch all rows from the dynamic_routes table
  $stmt = $pdo->query('SELECT `route`, `destination` FROM `dynamic_routes`;');

  $routes = [];

  while ($row = $stmt->fetch()) {
    $routes[$row['route']] = $row['destination'];
  }

  echo json_encode($routes);
} catch (PDOException $e) {
  http_response_code(500);
  echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}

5. Now alter `custom_routing.php' so it looks like this:
<?php

function fetch_dynamic_routes() {
    $url = BASE_URL . 'public/dr.php';
    // Initialize cURL session
    $ch = curl_init();
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Execute cURL session
    $jsonData = curl_exec($ch);
    // Close cURL session
    curl_close($ch);
    return $jsonData;
}

$routes = json_decode(fetch_dynamic_routes(), true);

define('CUSTOM_ROUTES', $routes);

Cheers,
Si

Full post on the SCA forum
https://ums.myds.me/phpbb/viewtopic.php?f=5&t=1330

This comment was edited by DaFa on Friday 24th May 2024 at 14:12 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by siri on Monday 27th May 2024 at 13:49 GMT

Hi Simon,
sorry for this and thank you, it is working .
Best Regards,
Siri K
Early Adopter

siri

User Level: Early Adopter

Date Joined: 19/07/2019

×