I want to have an ID in my URL and the controller of my module should take the ID from the URL and display it. If the ID is changed in the browser and the side is reloaded, the page should print out the new ID. My problem is, that I can't get the ID out of the URL.
Example: /node/31?id=930bcae8-f8e5-491f-84fc-cdf104ca7e40
I have tried to following lines of code, to get the id and print it out:
'text' => \Drupal::request()->query->all(), <- prints out nothing
'text' => $_SERVER['QUERY_STRING'], <- also prints out nothing
and
'text' => $_SERVER['REQUEST_URI'], <- was the most succsessfull one, with printing out /node/31
I would really appreciate any help, to help me figure out, how to get any id from the url?
Thanks in advance!
Here comes my whole controller code:
<?php
namespace Drupal\database_mindmap2\Controller;
use Drupal\Core\Controller\ControllerBase;
class database_mindmap2Controller extends ControllerBase {
public function query_easy() {
\Drupal\Core\Database\Database::setActiveConnection('external');
$database = \Drupal\Core\Database\Database::getConnection();
$staticID = "23e5bf9c-a8ca-46fd-b198-d4279559af32";
$query = $database->query('SELECT StaticIDs, CoreStatement, Text, PageRange FROM KnowledgeItem WHERE StaticIDs LIKE "%'.$staticID.'%"');
$result = $query->fetchAll();
$pagerange_all = explode(" ",$result[0]->PageRange);
$text = $result[0]->Text;
$heading = $result[0]->CoreStatement;
$pagerange = "Seitenzahl: ".$pagerange_all[9]." ".$pagerange_all[11];
\Drupal\Core\Database\Database::setActiveConnection();
return array(
'text' => $_SERVER['REQUEST_URI'],
'heading' => $heading,
'pagerange' => $pagerange
);
}
}And the code of my .routing.yml
I am using the module together with a content type, so I never use the path /database-mindmap2 which is given in the routing file. Is that a problem?
database_mindmap2.content:
path: '/database-mindmap2'
defaults:
_controller: '\Drupal\database_mindmap2\Controller\database_mindmap2Controller::query_easy'
_title: 'Hello World'
requirements:
_permission: 'access content'
Comments
Maybe the problem is in your
Maybe the problem is in your controller. Share the code of the controller and of the 'YOUR_MODULE.routing.yml'
To ensure that the "\Drupal::request()->query->all();" works you can use the code like this:
I added my code above.
I added my code above.
Into which file, should I insert code?
Into which file, should I
YOUR_MODULE.module
Yes.
Here are the useful links:
1. https://www.drupal.org/docs/drupal-apis/routing-system/introductory-drup...
2. https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-routes
3. https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/pa...
4. https://www.drupal.org/docs/drupal-apis/routing-system/altering-existing...
But if after reading the problem does not go away, then describe your use case in details and we will try to help.