0
votes

I am making my own cakephp 2 plugin called Rma and for this plugin I made some routes. When I want to make a form linking to the edit page, the link is different from the link in my routes file. In the browser are my routes working, but cakephp generate different links when I change form actions or echo the Html Link.

The code echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 2)); gives me the following url: '/Rma/RmaRequests/edit/2' while the url in the routes file is '/rma/edit/:id'. Cakephp gives me the wrong url.

This is my routes.php in the plugin config folder (app/Plugin/Rma/Config/routes.php):


Router::connect('/rma/new', [
    'plugin' => 'Rma',
    'controller' => 'RmaRequests',
    'action' => 'add'
]);
Router::connect('/rma/edit/:id', [
    'plugin' => 'Rma',
    'controller' => 'RmaRequests',
    'action' => 'edit'
],
    array(
        // order matters since this will simply map ":id" to
        // $articleId in your action
        'pass' => array('id'),
        'id' => '[0-9]+'
    ));

Router::connect('/rma/new/:id/', array(
    'plugin' => 'Rma', 'controller' => 'RmaRequestProducts', 'action' => 'add'),
    array(
        // order matters since this will simply map ":id" to
        'pass' => array('id'),
        'id' => '[0-9]+'
    )
);

I've also tried to place the route in my application routes.php, but that still doesn't solve the problem.

This is the way how I load my plugin in app/Config/bootstrap.php: CakePlugin::load('Rma', array('routes' => true, 'bootstrap' => true));

Is there something I forgot or something that I did wrong? I am using CakePHP version 2.5.1

1
You probably have other routes connected earlier that are matching that URL array, probably some catch-all routes like /:controller/:action/*.ndm
I found one route like that. I removed that route but the url is still the same. I've also changed the route from /rma/... to /rmaplugin/.... but it still doesn't workSander van Hoogdalem
If the URL is still the same, then there's very likely still a route that catches it. You'll have to check all your routes (search the whole codebase for Router::connect), and show them all if you can't figure it out.ndm
Here can you find my complete routes.php file (app/Config/routes.php): pastebin.com/Pb7VK5kt. The theme has no routes by the waySander van Hoogdalem
Sorry, I haven't used CakePHP 2 in a long time, and totally forgot how it handles named parameters and non-matching routes, it requires you to use the name in the URL array, and it will not throw an error for non matching routes, but generate a fallback URL :( Long story short, problem is probably that you should use 'id' => 2 in your URL array, not just 2.ndm

1 Answers

0
votes

The answer, thnx to @ndm in the comments:

The Url has an named parameter called id. Instead of: echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 2));

It must be

echo $this->Html->url(array('plugin' => 'Rma', 'controller' => 'RmaRequests', 'action' => 'edit', 'id'=> 2));