如何检测当前URL?


Answers:


6

您应该使用以下代码,作为url()返回作为参数传递的路径的URL;因此,它返回IF语句认为等同的值TRUE(字符串为"0"或为空字符串的情况除外)。

if ($_GET['q'] == 'the/internal/Drupal/path') {
  // Do stuff
}

您可能还需要drupal_get_path_alias($ _ GET ['q'])来检查源/目标路径别名。
cam8001 2011年

12

Drupal的别名是您想要的

<?php
$path = drupal_get_path_alias($_GET['q']);
if ($path == 'the/path') {
  // do stuff
}
?>

以下其他:

完整网址

<?php
global $base_root;
$base_root . request_uri()
?>

Drupal的“内部” URL

<?php
$arg = arg();
// Path of 'node/234' -> $arg[0] == 'node' && $arg[1] == 234
?>

arg(); 是适合我的一个。干杯;)
Daithí12年

4

Drupal 7

// Retrieve an array which contains the path pieces.
$path_args = arg();

// Check if the current page is admin.
if (arg(0) == 'admin') {
  // This is wrong even in D7. path_is_admin() should be used instead.
}

// Conditionally add css or js in certain page.
function mymodule_page_build(&$page) {
  if (arg(0) == 'sth' && arg(1) == 'else') {
    drupal_add_css(drupal_get_path('module', 'mymodule') . '/css/my.css');
  }
}

// Load the current node.
if (arg(0) == 'node' && is_numeric(arg(1))) {
  // This is wrong even in D7. menu_get_object() should be used instead.
}

Drupal 8(过程性)

// Retrieve an array which contains the path pieces.
$path_args = explode('/', current_path());

// Check if the current page is admin.
if (\Drupal::service('router.admin_context')->isAdminRoute(\Drupal::routeMatch()->getRouteObject())) {
}

// Conditionally add css or js in certain page.
function mymodule_page_build(&$page) {
  if (\Drupal::routeMatch()->getRouteName() == 'my.route') {
    $page['#attached']['css'][] = drupal_get_path('module', 'mymodule') . '/css/my.css';
  }
}

// Load the current node.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
}

Drupal 8(OOP)

use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
class myClass {
  public function __construct(Request $request, AdminContext $admin_context, RouteMatchInterface $route_match) {
    $this->request = $request;
    $this->adminContext = $admin_context;
    $this->routeMatch = $route_match;
  }

  public function test() {
    // This might change in https://drupal.org/node/2239009
    $current_path = $this->request->attributes->get('_system_path');
    // Retrieve an array which contains the path pieces.
    $path_args = explode('/', $current_path);

    // Check if the current page is admin.
    if ($this->adminContext->isAdminRoute($this->routeMatch->getRouteObject())) {
    }

    // Load the current node.
    $node = $this->routeMatch->getParameter('node');
    if ($node) {
    }
  }
}

来源:arg()已过时,将在Drupal.org 上删除



2

您将要使用arg()函数。您可以使用以下两种方法之一,

$args = arg();

基本上,这将为您提供一个数组,每个url参数是另一个值,或者您可以像这样检查特定的参数:

$arg = arg(0);

因此,对于您的示例,您可以执行以下操作:

if(!is_null(arg(1)) && arg(0) == 'the' && arg(1) == 'path') { Do something  }

或者我建议这样做:

$args = arg();
if(!empty($args[1]) && $args[0] == 'the' && $args[1] == 'path') { Do something  }

您还可以使用$arg = implode('/',arg());
tim.plunkett 2011年

1

如果您不想为具有特定URL的页面使用其他页面模板,则可以使用以下代码检查当前URL。

if (arg(0) == 'the' && arg(1) == 'path') {
  // Add the extra CSS class.
}

url()不是返回当前页面URL的函数;如果在url()不提供任何参数的情况下进行调用,您将(至少在Drupal 7上且未实现任何模块hook_ulr_outbound_alter())获得Drupal安装的基本URL。如果没有模块更改该函数返回的值,则
调用url('the/path')只会返回您"the/path"。这意味着将始终执行您显示的代码,并始终添加CSS类。


0

如果您正在寻找Drupal服务的规范路径,则可以点击$ _GET ['q'],即使Drupal使用干净的URL,它也应该翻译。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.