In drupal 7 I have a chunk of code that gets the page content by mimicking what drupal does:

$router_item = menu_get_item($href);
if ($router_item['access']) {
  if ($router_item['include_file']) {
    require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
  $page_callback_result = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);

$page_callback_result will then hold the HMTL of the content region of that page.

I'm trying to port this to drupal 8 but I'm stuck. Anyone has a clue how to do this? I'm close, but I'm struggling on getting the controller:

 $request = Request::create('/' . $href);
 $controller_resolver = \Drupal::getContainer()->get('controller_resolver');
 //$controller = $controller_resolver->getController($request);
 $callable = $controller_resolver->getControllerFromDefinition('\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage');
 $arguments = $controller_resolver->getArguments($request, $callable);
 $page_content = call_user_func_array($callable, $arguments);

The problem is that $controller = $controller_resolver->getController($request); returns FALSE. I'm trying to get the proper controller for a request, for some reason this won't work...


I'm not sure what is the purpose of this, now in Drupal 8 Guzzle is available to creating external and internal request.


The httpClient is a service in Drupal 8 api.drupal.org/api/drupal/core%21core.services.yml/service/http_client/8

you need inject this service in your class.

if you need read the Route properties like callback, permissions, etc. you need use the routeProvider service.


maybe more information about that problem help us to understand what is the best way to porting for Drupal 8


Thinking a little more, you want to make a sub-request?

something like https://gist.github.com/dmouse/4976461875a27689d155

  • Looks like this is exactly what I need! I'll try this out tonigh, thanks!
    – Robin
    Oct 6 '14 at 7:17
  • Your code gets me the HTML of the body, but this isn't really what I need. I just need the HTML of the content of that page. In other words I have to mimic the following code that drupal executes in core: $response = call_user_func_array($controller, $arguments); As you see in my code example above I'm trying to get the controller and the arguments, but for some reason they are both NULL
    – Robin
    Oct 6 '14 at 12:45
  • @Robin Why not put some debug code in to inspect the $request for a normal page, that seems like the easiest way to find out what your own $request is missing?
    – Clive
    Oct 6 '14 at 12:50
  • @Clive, My request object is ok, it is this line of code that causes trouble: $controller = $controller_resolver->getController($request)
    – Robin
    Oct 6 '14 at 12:51
  • That's my point - $request->attributes->get('_controller') is obviously empty, when it shouldn't be (api.drupal.org/api/drupal/…). Check your log - you'll have an error message like this: "Unable to look for the controller as the "_controller" parameter is missing"
    – Clive
    Oct 6 '14 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.