Category: Routing

Q: Can't get query strings from URL

This my code:

<?php 


public function getUser() {
  if ($this->Auth->user()) {
    $userId = (empty($this->request->query['id']) ? null : $this->request->query['id']);
    if ($userId == null) {
      $user = $this->Data->findUser($userId);
      $this->set('user', $user);
    } else {
      $this->set('msg', 'user not found');
      $this->set('user', null);
    }

    $this->viewBuilder()->layout('template');
  } else {
    $this->redirect(['controller' => 'users', 'action' => 'login']);
  }
}

My problem is whenever I pass id to this action, I will get $userId = null so that the if/else failed to work. Can someone help me?

updated:

beforeFilter looks like this:

public function beforeFilter(Event $event) {
	parent::beforeFilter($event);
	$this->Auth->allow(['getUser']);

	$this->Security->requireSecure();
}
SOLVED query string url
1 Answers
Accepted by Faalih Safi

Typically when you use Security->requireSecure() to do https redirects, you have to make sure you do pass all data from http to https.

public function beforeFilter(Event $event) {
	parent::beforeFilter($event);
	$this->Auth->allow(['getUser']);

	$this->Security->requireSecure(); //check this out
}

and more for example:

you make have the following code in controller's initialize function:

$this->loadComponent('Security', ['blackHoleCallback' => 'ssl']);

make sure you have ssl() function looks like:

public function ssl() {

  return $this->redirect('https://' . env('SERVER_NAME') . $this->request->here());
  //$this->request->here() will pass full url with query strings
  //$this->request->here will echo only base path of url
}

Hope this helps

Created: 31 Aug '16

Last Reply: 3 Sep '16

Replies: 1

Views: 529

Votes: 0

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions