Réécriture d'url

Par gilbert1995, il y a 12 ans


Bonjour,
dans mon fichiers routes.php j'ai

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'accueil'));
        Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
    Router::connect('/downloads/:id/:key',
        array(
            'controller' => 'panels',
            'action' => 'downloads'
        ),
        array(
            'pass'=>array('id','key'),
            'id' => '[0-9]+'
        )
    );
    Router::connect('/panels', array('controller' => 'panels', 'action' => 'profile'));

dans ma view je fait la commande

$this->Html->url(array('controller' => 'panels', 'action' => 'downloads', 47, "WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx"),true)

pour avoir l'url de téléchargement mais ma règle de routage n'est pas pris en compte.

J’obtiens : http://xxx.com/panels/downloads/47/WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx
Alors que l'url que je veux est : http://xxx.com/downloads/47/WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx

Que dois-je faire
Merci

25 réponses

antho07, il y a 12 ans

bah c'est bon là ..

'id' => '96',
'key' => 'auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'

quel est le soucis??

Kareylo, il y a 12 ans
$this->Html->url(array('controller' => 'panels', 'action' => 'downloads', 'id' => 47, 'key' => "WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx"),true)
gilbert1995, il y a 12 ans

Kareylo ca ne change rien :(

ker0x, il y a 12 ans

En plus d'utiliser le code de Kareylo, il faut que tu précises à ton Router à quoi doit ressembler la clé Key

Router::connect('/downloads/:id/:key',
    array(
        'controller' => 'panels',
        'action' => 'downloads'
    ),
    array(
        'pass'=>array('id','key'),
        'id' => '[0-9]+',
        'key' => '[a-zA-Z0-9]+'
    )
);
gilbert1995, il y a 12 ans

Bon ca ne marche toujours pas voici mon tableau d'argument pour $this->Html->url

array('controller' => 'panels', 'action' => 'downloads', 'id'=>45, 'key'=>'WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx')

Et mon ficher routes.php

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'accueil'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/downloads/:id/:key', array(
    'controller' => 'panels',
    'action' => 'downloads'
        ), array(
    'pass' => array('id', 'key'),
    'id' => '[0-9]+',
    'key' => '[a-zA-Z0-9]+'
        )
);
Router::connect('/panels', array('controller' => 'panels', 'action' => 'profile'));

CakePlugin::routes();

require CAKE . 'Config' . DS . 'routes.php';

Merci beaucoup de votre aide

Kareylo, il y a 12 ans

Est-ce que ta fonction downloads est bien comme ceci ?

public function downloads($id = null, $key = null){
...
}
antho07, il y a 12 ans

Bonsoir, tente ça:

$this->Html->url(array("controler"=> truc, 'action'=>truc, array('id machin choutte), 'panels' => false)

gilbert1995, il y a 12 ans

Négatif antho07

Autre précision ce code

Router::connect('/downloads/*',array('controller' => 'panels','action' => 'downloads'));

me donnais bien mon lien .../downloads/47/hakjahskdjhas mais il ne vérifie pas si 47 est bien un chiffre...

PS: voici le debug du tableau push dans la function:

array (size=4)
  'controller' => string 'panels' (length=6)
  'action' => string 'downloads' (length=9)
  'id' => int 94
  'key' => string 'B4v95mNlzZFKt4PPfLJikrpzmvvLjgnX' (length=32)

Gilbert

antho07, il y a 12 ans

Ah mais ok ya du routing...
et ça: ?

Router::connect('/downloads/:id/:key',array('controller'=>'panels','action'=>'downloads'),array('pass'=>array('id','key'),'id'=>'[0-9]+','key'=>'[0-9a-zA-Z]+'));
gilbert1995, il y a 12 ans

Négatif antho07

gilbert1995, il y a 12 ans

oui

antho07, il y a 12 ans

Dans ton controller fait un debug($this->request) pour voir comment il parse ton url

antho07, il y a 12 ans

'action' => 'files', ???

gilbert1995, il y a 12 ans

oups pas le bon debug :D

http://xxx.com/bob/panels/downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz

object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'panels',
        'action' => 'downloads',
        'named' => array(),
        'pass' => array(
            (int) 0 => '96',
            (int) 1 => 'auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
        ),
        'isAjax' => false
    )
    data => array()
    query => array()
    url => 'panels/downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
    base => '/bob'
    webroot => '/bob/'
    here => '/bob/panels/downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
    [protected] _detectors => array(
        'get' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'GET'
        ),
        'post' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'POST'
        ),
        'put' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'PUT'
        ),
        'delete' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'DELETE'
        ),
        'head' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'HEAD'
        ),
        'options' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'OPTIONS'
        ),
        'ssl' => array(
            'env' => 'HTTPS',
            'value' => (int) 1
        ),
        'ajax' => array(
            'env' => 'HTTP_X_REQUESTED_WITH',
            'value' => 'XMLHttpRequest'
        ),
        'flash' => array(
            'env' => 'HTTP_USER_AGENT',
            'pattern' => '/^(Shockwave|Adobe) Flash/'
        ),
        'mobile' => array(
            'env' => 'HTTP_USER_AGENT',
            'options' => array(
                (int) 0 => 'Android',
                (int) 1 => 'AvantGo',
                (int) 2 => 'BlackBerry',
                (int) 3 => 'DoCoMo',
                (int) 4 => 'Fennec',
                (int) 5 => 'iPod',
                (int) 6 => 'iPhone',
                (int) 7 => 'iPad',
                (int) 8 => 'J2ME',
                (int) 9 => 'MIDP',
                (int) 10 => 'NetFront',
                (int) 11 => 'Nokia',
                (int) 12 => 'Opera Mini',
                (int) 13 => 'Opera Mobi',
                (int) 14 => 'PalmOS',
                (int) 15 => 'PalmSource',
                (int) 16 => 'portalmmm',
                (int) 17 => 'Plucker',
                (int) 18 => 'ReqwirelessWeb',
                (int) 19 => 'SonyEricsson',
                (int) 20 => 'Symbian',
                (int) 21 => 'UP\.Browser',
                (int) 22 => 'webOS',
                (int) 23 => 'Windows CE',
                (int) 24 => 'Windows Phone OS',
                (int) 25 => 'Xiino'
            )
        ),
        'requested' => array(
            'param' => 'requested',
            'value' => (int) 1
        )
    )
    [protected] _input => ''
}

http://xxx.com/bob/downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz

object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'panels',
        'action' => 'downloads',
        'named' => array(),
        'pass' => array(
            (int) 0 => '96',
            (int) 1 => 'auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
        ),
        'id' => '96',
        'key' => 'auleBDYMwq1VtaoeSbI7gr4QaWb95qBz',
        'isAjax' => false
    )
    data => array()
    query => array()
    url => 'downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
    base => '/bob'
    webroot => '/bob/'
    here => '/bob/downloads/96/auleBDYMwq1VtaoeSbI7gr4QaWb95qBz'
    [protected] _detectors => array(
        'get' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'GET'
        ),
        'post' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'POST'
        ),
        'put' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'PUT'
        ),
        'delete' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'DELETE'
        ),
        'head' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'HEAD'
        ),
        'options' => array(
            'env' => 'REQUEST_METHOD',
            'value' => 'OPTIONS'
        ),
        'ssl' => array(
            'env' => 'HTTPS',
            'value' => (int) 1
        ),
        'ajax' => array(
            'env' => 'HTTP_X_REQUESTED_WITH',
            'value' => 'XMLHttpRequest'
        ),
        'flash' => array(
            'env' => 'HTTP_USER_AGENT',
            'pattern' => '/^(Shockwave|Adobe) Flash/'
        ),
        'mobile' => array(
            'env' => 'HTTP_USER_AGENT',
            'options' => array(
                (int) 0 => 'Android',
                (int) 1 => 'AvantGo',
                (int) 2 => 'BlackBerry',
                (int) 3 => 'DoCoMo',
                (int) 4 => 'Fennec',
                (int) 5 => 'iPod',
                (int) 6 => 'iPhone',
                (int) 7 => 'iPad',
                (int) 8 => 'J2ME',
                (int) 9 => 'MIDP',
                (int) 10 => 'NetFront',
                (int) 11 => 'Nokia',
                (int) 12 => 'Opera Mini',
                (int) 13 => 'Opera Mobi',
                (int) 14 => 'PalmOS',
                (int) 15 => 'PalmSource',
                (int) 16 => 'portalmmm',
                (int) 17 => 'Plucker',
                (int) 18 => 'ReqwirelessWeb',
                (int) 19 => 'SonyEricsson',
                (int) 20 => 'Symbian',
                (int) 21 => 'UP\.Browser',
                (int) 22 => 'webOS',
                (int) 23 => 'Windows CE',
                (int) 24 => 'Windows Phone OS',
                (int) 25 => 'Xiino'
            )
        ),
        'requested' => array(
            'param' => 'requested',
            'value' => (int) 1
        )
    )
    [protected] _input => ''
}
antho07, il y a 12 ans

Bizarre cela fonctionne chez moi:

Router::connect('/downloads/:id/:key',
    array(
        'controller' => 'panels',
        'action' => 'downloads'
    ),
    array(
        'pass'=>array('id','key'),
        'id' => '[0-9]+',
        'key' => '[a-zA-Z0-9]+'
    )
);

echo Router::url(array('controller' => 'panels', 'action' => 'downloads', 'id'=>45, 'key'=>'WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx'));
affiche bien /projet/downloads/45/WSvkx0JLvYCuPZOQdvzRy3c6rxoeR4Zx

antho07, il y a 12 ans

Ah bah j'avais pas fait attention...

Il ne faut pas utiliser la fonction url du helper html qui elle va te fabriquer une url avec les paramètres que tu lui donnes (du moins je pense au vu du résultat)

mais la fonction url de la classe static Router

qu'on appelle comme cela: Router::url

gilbert1995, il y a 12 ans

Pour info $this->Html->url est un alias de Router::url

gilbert1995, il y a 12 ans

Merci à tous de votre aide !

antho07, il y a 12 ans

C'est réglé??

antho07, il y a 12 ans

Possible, pour $this->Html->url, mes souvenirs de cake sont assez loin..

gilbert1995, il y a 12 ans

Oui c'est réglée...