Get URL parameters inside custom module

45,991

Solution 1

Use \Drupal\Core\Routing;:

$parameters = \Drupal::routeMatch()->getParameters();

The named parameters are available as

$value = \Drupal::routeMatch()->getParameter('slug_name_from_route');

Where 'slug_name_from_router' comes from your routing.yml path property

path: '/your/path/{slug_name_from_route}'

If you want the raw parameter without any upcasting you can get

$value = \Drupal::routeMatch()->getRawParameter('slug_name_from_route');

Solution 2

I used to get the parameter value from URL (localhost/check/myform?mob=89886665)

$param = \Drupal::request()->query->all();

And applied in my select Query

 $query = \Drupal::database()->select('profile_register', 'p1');
 $query->fields('p1');
 $query->condition('p1.mobileno', $edituseprof);
 $query->condition('publishstatus', 'no');
 $result = $query->execute()->fetchAll();

But on multiple parameter value, i am now successful(Ex: http://10.163.14.41/multisite/check/myform?mob=89886665&id=1)

 $query = \Drupal::database()->select('profile_register', 'p1');
 $query->fields('p1');
 $query->condition('p1.mobileno', $edituseprof['mob']);
 $query->condition('p1.ids', $edituseprof['id']);
 $query->condition('publishstatus', 'no');
 $result = $query->execute()->fetchAll();

Solution 3

arg() is deprecated in drupal 8, however we can get values like arg() function does in drupal 7 & 6

$path = \Drupal::request()->getpathInfo();
$arg  = explode('/',$path);
print_r($arg); exit(); 

The output would be parameters in url except basepath or (baseurl),

Array
(
   [0] => 
   [1] => node
   [2] => add
)

Solution 4

To get query parameter form the url, you can us the following. If you have the url for example,

domainname.com/page?uid=123&num=452

To get "uid" from the url, use..

$uid = \Drupal::request()->query->get('uid');

To get "num" from the url, use..

$num = \Drupal::request()->query->get('num');

Solution 5

$route_match = \Drupal::service('current_route_match');
$abc = $route_match->getParameter('node'); //node is refrence to what you have written in you routing file i.e: 

in something.routing.yml
entity.node.somepath:
  path: '/some/{node}/path'

I have used {node} as arg(1). And I can access it by using *->getParameter('node');

Hope this will work.

Share:
45,991
zied123456
Author by

zied123456

Updated on November 28, 2020

Comments

  • zied123456
    zied123456 over 3 years

    I've created a custom block like this:

    class HelloBlock extends BlockBase implements BlockPluginInterface{
    
      /**
       * {@inheritdoc}
       */
      public function build() {
        $config = $this->getConfiguration();
        $result = db_query('SELECT * FROM {test}');
        return array(
          '#theme' => 'world',
          '#test' => $result
        );
      }
    }
    

    And I now want to programmatically get some parameter from the URL.

    For example:

    If the URL is http://localhost/drup/hello/5569 I want to get hold of the value 5569 inside my module.

    I have tried arg(1) and drupal_get_query_parameters() but I got this error messages:

    Call to undefined function `Drupal\hello\Plugin\Block\arg()`
    

    and

    Call to undefined function `Drupal\hello\Plugin\Block\drupal_get_query_parameters()`
    

    How can I get the parameters?

  • leymannx
    leymannx over 5 years
    drupal.org/node/2274705 says $path = \Drupal::service('path.current')->getPath();. But I don't see much of a difference actually.
  • anoopjohn
    anoopjohn about 2 years
    Given that we are able to get named parameters values from request match, we shouldn't use arg and explode. The problem is that we will run into a lot of difficulty if we have to change URL structures later and insert additional path components.