简体   繁体   中英

How can I do this route properly in CakePHP?

I'm trying to create a route and I'm having some difficulty getting it to function.

I have this action

// MembershipsController
public function professional_info($type = null, $member_id = null) {
  // Work done here.
}

This is easily accessible via the url /memberships/professional-info/mytype/{user-id-here}

However, I would like to change this url to:

/mytype-professional-info/{user-id-here}

For this reason, I am trying to create a route that automatically passed mytype as the first argument to professional_info , and then accepts a numeric user id from the url, passing that as the second argument.

I have tried stuff similar to the following:

Router::connect('/mytype-professional-info/:id',
    array('controller' => 'memberships', 'action'=> 'professiona_info', 'mytype'),
    array(
        "pass" => array('id'),
        "id" => '[0-9]+',
    )
);
  • I am not certain how to set this route up correctly. Could someone please explain to me how I can create a route that passes static values for some arguments, and dynamic values from the url for other arguments?

Thank you very much.

The way you've defined the route it will pass the type, but as the second argument.

If this is not acceptable for whatever reason, then you'll have to explicitly pass the paramter, this however works only using named elements:

Router::connect(
    '/mytype-professional-info/:id',
    array(
        'controller' => 'memberships',
        'action'=> 'professional_info',
        'type' => 'mytype'
    ),
    array(
        'pass' => array('type', 'id'),
        'id' => '[0-9]+',
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM