We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

User id parameter

Hello,

why are my models like {{ company.name }}, {{ user.name }} and {{ region.name }} undefined?

I have a link with an user id:

 {{ link_to("users/setcard?setcard=" ~ user.id, '<i class="icon-fast-forward"></i> Last', "class": "btn") }}
  https://localhost:8000/projects/vokuro/users/setcard?setcard=1

This is my UserController.php:

public function setcardAction(){

    if ($this->request->isPost()) {
        $query = Criteria::fromInput($this->di, 'Vokuro\Models\Users', $this->request->getPost());
        $this->persistent->searchParams = $query->getParams();
    } else {
        $this->request->getQuery("setcard", "int");
    }

    $parameters = array();
    if ($this->persistent->searchParams) {
        $parameters = $this->persistent->searchParams;
    }

    $users = Users::find($parameters);
    if (count($users) == 0) {
        $this->flash->notice("The search did not find any users");
        return $this->dispatcher->forward(array(
            "action" => "index"
        ));
    }
}

And the setcard.volt, ok this is not so much right now.

{{ content() }}

Setcard {{ user.name }}

My SetcardController.php:

<?php
namespace Vokuro\Controllers;

class SetcardController extends ControllerBase
{

public function initialize()
{
    $this->view->setTemplateBefore('private');
}

}

Kind Regards

Stefan

edited Aug '15

Local variables in an action won't get passed to the view, you need to assign them explicitly:

//controller
public function someAction($id)
{
    $this->view->user = User::findFirst($id);
}


60.0k

Hi Lajos,

it works halfway :-\

i get my name, but i also get Warnings:

Warning: Missing argument 1 for Vokuro\Controllers\UsersController::setcardAction() in C:\xampp\htdocs\projects\vokuro\app\controllers\UsersController.php on line 72

Notice: Undefined variable: id in C:\xampp\htdocs\projects\vokuro\app\controllers\UsersController.php on line 73

Setcard stefan - germany

 public function setcardAction($id){
    $this->view->user = Users::findFirst($id);
}

Rgds

Stefan



43.9k
edited Sep '15

Hi,


public function setcardAction($id){
   $id = $this->request->get('id');
    $this->view->user = Users::findFirst($id);
}

@Stefjoe what does the request URL look like?

  • /user/setcard/123: you have to set up the proper routing too. ($router->add(/user/setcard/:id), array(controller=>user, action=>setcard))
  • /user/setcard/?id=123: use the code shown by @le51


60.0k

Hi le51 and Lajos,

i haven't done something with the router, that will be the next step

It looks like this:

https://localhost:8000/projects/vokuro/users/setcard?setcard=1

My Volt:

{{ link_to("users/setcard?setcard=" ~ user.id, '<i class="icon-fast-forward"></i> Last', "class": "btn") }}

Thank you so much for your help :-)

Rgds

Stefan



77.7k
Accepted
answer

Then it should be

public function setcardAction(){
   $id = $this->request->get('setcard');
    $this->view->user = Users::findFirst($id);
}


60.0k

Hi Lajos,

awesome, it works fantastic.

What can i do with the router?

I have tried this:

$router->add('/setcard/{code}', array(
'controller' => 'user_control',
'action' => 'setcard'
));

Rgds

Stefan

Routing is a bit extensive, check out the docs: https://docs.phalcon.io/en/latest/reference/routing.html

But for this case, you'd probably do something like this:

$router->add('/setcard/{code:[0-9]+}', array(
    'controller' => 'user_control',
    'action' => 'setcard',
));
public function setcardAction()
{
    $this->view->user = User::findFirst($this->dispatcher->getParam('code'));
}


60.0k

Hi Lajos,

i tried to do your routing advice, sorry but with no success:

URL:

https://localhost:8000/projects/vokuro/sedcard?sedcard=2

route.php

$router->add('/sedcard/{code:[0-9]+}', array(
'controller' => 'sedcard',
'action' => 'index',
));

SedcardController.php

<?php
namespace Vokuro\Controllers;
use Vokuro\Models\Users;

class SedcardController extends ControllerBase
{

/**
 * Default action. Set the public layout (layouts/public.volt)
 */
public function initialize()
{
    $this->view->setTemplateBefore('private');
}
public function indexAction(){
    $id = $this->request->get('sedcard');
    $this->view->user = Users::findFirst($id);

    //$this->view->user = Users::findFirst($this->dispatcher->getParam('code'));
}

}

Thx again :-)