5

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

1
  • have you define your model Url?? Commented Feb 23, 2016 at 4:45

2 Answers 2

13

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.

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

2 Comments

"url":"http:\/\/localhost\/bookingsev\/company\/1-Moe Moe\/service\/2-face washing\/booking\/1-@2\/20\/16, 12:00 PM-2\/20\/16, 1:00 PM is the url output.
congrats it worked. now you should vote up the answer. for more info about Routing visit : book.cakephp.org/3.0/en/development/routing.html
1

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]);
    ...
}

Comments

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.