0

I'm building a prototype search application using CakePHP and I have a search controller that has the following code so far:

class SearchController extends AppController
{
    var $name = 'Search';

    var $uses = array();

    function index ( $query )
    {
        $query = $_GET['q'];

        $this->set('title_for_layout', $query. ' – smplr');

        // if no query then redirect to home
        if ( !$query )
        {
            $this->redirect(array('controller' => 'home', 'action' => 'index'));
        }
    }
}

So this would be fired when a user has something like this: domain.com/search?q=Hello

My question is how to use the query passed into index() ?

At the moment I have to manually create a new variable to grab the query string using $_GET which I'm sure is NOT the way to do this using Cake.

Any help much appreciated. Thanks

0

1 Answer 1

1

You need to use:

$this->params['url']['q'];

More specifically:

function index ( $query )
    {
        $query = $this->params['url']['q'];

....
Sign up to request clarification or add additional context in comments.

6 Comments

So I wouldn't need the $query inside the index() at all then? As I could just use the variable content straight from the params?
Yes, you wouldn't need to catch $query as a function argument.
Why not though? When dealing with normal functions like creating, editing etc you would do. Thanks
Because you're not passing it the normal way. Your url is: domain.com/search?q=asdf. To catch it how you want to, your url should be: domain.com/search/index/asdf
What's the difference between $query = $this->params['url']['q']; and $query = $_GET['q'];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.