How to build a url in Controller (solved)

Solved,
I found how to build the url.
This should do the job:
`

$targeturl=Router::url(‘/’, true).strtolower($this->request->getParam(‘prefix’));

`

Hello,
I’m trying to integrate a live search with clickable search results into my app using Ajax.
I followed the example from w3schools (https://www.w3schools.com/php/php_ajax_livesearch.asp).
The search works, but I have problems displaying the search results.
I have this line of code to build the search result:

$hint='<a href="/terms/view/'. $term->id.'"' . 'class="button" target="_blank">'.$term->name.'</a>';

This line produces a link to

> "localhost/terms/view/2"

What I need is a link to "localhost/termbase/customer/user/terms/view/2"

“Customer” and “User” are prefixes

I hope i could explain what I try to do… and anyone can give me a hint.

Here is the full code from the controller

>     public function searchterm() {
>       $this->autoRender=false;
>       //get the q parameter from URL
>       $q=$_GET["q"];
> 
>       //lookup all links from the xml file if length of q>0
>       if (strlen($q)>0) {
>         $hint="";
>         $query=$this->Terms->find()
>           ->select(['id','name'])
>           ->contain(['Concepts'])
>           ->where(['terms.name like'=>'%'.$q.'%','concepts.customer_id'=>$this->request->getAttribute('identity')->customer_id])
>           ->order(['Terms.name'=>'ASC'])
>           ->limit(10);
>           if ($query->count() > 0) {
> 
>               foreach ($query as $term) {
>                 $y=$term->name;
>                 $z=$term->id;
>                 //echo $this->Html->link($term->name,['controller' => 'Terms', 'action' => 'view',$term->id, '_full' => true]);
>                 if ($hint=="") {
>                   $hint='<a href="/terms/view/'. $term->id.'"'  . 'class="button" target="_blank">'.$term->name.'</a>';
> 
>                 } else {
>                   $hint=$hint . "<br /><a href='" .
>                   $z .
>                   "' target='_blank'>" .
>                   $y . "</a>";
>                 }
>               }
>           }
>           if ($hint=="") {
>             $response=__('No Results');
>           } else {
>             $response=$hint;
>           }
>         //output the response
>         echo $response;
> 
>     }
>   }

Wouldn’t something like this work?

$this->Html->link($term->name, ['controller' => 'Terms', 'action' => 'view', $term->id, 'prefix' => $this->request->getParam(‘prefix’), '_full' => true])

This gives me an Error

Call to a member function link() on null

Oh, because it’s in a controller, not a template. I copied your commented out code as my starting point.

$url = Cake\Routing\Router::url(['controller' => 'Terms', 'action' => 'view', $term->id, 'prefix' => $this->request->getParam(‘prefix’), '_full' => true])

should give you the URL?

1 Like

Thank you very much for your help.
I had to add a Backslash at the beginning.
I think this is because iam in the Namespace “customer/user” and the class “Router” was not found.

But this works:

$url = \Cake\Routing\Router::url([‘controller’ => ‘terms’, ‘action’ => ‘view’, $term->id,‘prefix’=>$this->request->getParam(‘prefix’),‘_full’ => true]);

Thanks again!

1 Like