Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 3 url in controller

I have to send url from controller to view with json..It not ok when i use $this->Html->link or $this->Url->build for link. How can i send link to json..

use Cake\View\Helper\UrlHelper; at top

foreach($schedules as $s) :
            $start=$s->start_at;
            $end=$s->finished_at;

   $link= $this->Url->build([ 
                                    'controller' => 'Bookings','action' => 'makebooking',
                                    'comid' => 1,
                                    'comslug' =>'Moe Moe',
                                    'sevid'=>2,
                                    'sevslug'=>'face washing',
                                    'sid'=>$s->id,
                                    'start'=>$start,
                                    'end'=>$end
                                    ]); 
            $out[]=array(
                'id'=>$s->id,
                'title'=>'( '.$s->start_at.'-'.$s->finished_at.' )'.$s->name,
                'url'=>$link,
                'start'=>strtotime($s->start_at).'000',
                'class'=>'event-important'

                );
         endforeach;


         echo json_encode(array('success'=>1,'result'=>$out));exit;

Error: Call to a member function build() on a non-object and i was

like image 732
Ye Htun Z Avatar asked Feb 23 '16 04:02

Ye Htun Z


2 Answers

use this code to generate url anywhere in the app:

use Cake\Routing\Router;

    $link =  Router::url([ 
                                        'controller' => 'Bookings','action' => 'makebooking',
                                        'comid' => 1,
                                        'comslug' =>'Moe Moe',
                                        'sevid'=>2,
                                        'sevslug'=>'face washing',
                                        'sid'=>$s->id,
                                        'start'=>$start,
                                        'end'=>$end
                                        ]);

To generate the full URL pass second arg as TRUE. see example below:

 $fullLink =  Router::url([ 
                                            'controller' => 'Bookings','action' => 'makebooking',
                                            'comid' => 1,
                                            'comslug' =>'Moe Moe',
                                            'sevid'=>2,
                                            'sevslug'=>'face washing',
                                            'sid'=>$s->id,
                                            'start'=>$start,
                                            'end'=>$end
                                            ],TRUE);

I have not tested the above code but I am sure it will work. Let me know if it does not work.

like image 123
Faiyaz Alam Avatar answered Oct 20 '22 00:10

Faiyaz Alam


You can also import the HtmlHelper class and use it in your controller

use Cake\View\Helper\HtmlHelper; 
....
public function register() {
    $html = new HtmlHelper(new \Cake\View\View());
    $homeLink = $html->link('Home', ['controller' => 'Pages', 'action' => 'home', '_full' => true]);
    ...
}
like image 22
zipate Avatar answered Oct 20 '22 00:10

zipate