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

Trailing Slashes in URL

I'm having a trouble regarding the trailing slashes when i used this code

$this->response->redirect( array( 'for' => 'admin', 'controller' => 'test' ));

and it gives me this "https://localhost/admin/test//" and what i want is "https://localhost/admin/test" and if I add this

$this->response->redirect( array( 'for' => 'admin', 'controller' => 'test', 'action' => 'index' ));

the result is this "https://localhost/admin/test/index/" , it should be like this "https://localhost/admin/test/index" .

My question is how can i remove the trailing slashes and how can I append ".html" as its extension?

"https://localhost/admin/test/index.html" or "https://localhost/admin/test.html"

I don't know for sure, but perhaps it is the routes you've set up?



16.3k
$router = new \Phalcon\Mvc\Router();

//Remove trailing slashes automatically
$router->removeExtraSlashes(true);

To: Sum

I already tried that but it only applies if I'm getting the address using the url/request module of phalcon. What I want is to be remove from the address bar automatically when I used this code:

$this->response->redirect( array( 'for' => 'admin', 'controller' => 'test' ));

The result should be like this "https://localhost/admin/test/index.html".

To: quasipickle

I'm figuring out what do you mean in my routers set up..



16.3k
Accepted
answer

hi, can provide your route. I confirm the problem exists when i add route with parameters and just redirect it without parameters. eg:

$router->add('/admin/{foo}/{bazz}', array(
    'controller' => 'user_control',
    'action' => 'moo'
))->setName('admin');
//...
// and just redirect without parameters
// u see i guess that is at least 1 paremeter (bazz)
$this->response->redirect( array( 'for' => 'admin'));
//yes it will give extra slashes because foo or bazz not filled

//but if the route is
$router->add('/admin, array(
    'controller' => 'user_control',
    'action' => 'moo'
))->setName('admin');
//just work

Hi Sum tnx for your help, I already fix the problem in trailing slashes by this code

$router->add('/admin/:controller/:action/:params', array( 'controller' => 1, 'action' => 2 'param' => 3 ))->setName('admin-full');

$router->add('/admin/:controller/:action', array( 'controller' => 1, 'action' => 2 ))->setName('admin-action');

$router->add('/admin/:controller', array( 'controller' => 1, ))->setName('admin-controller');

The problem was I have set the name of the router the same to each other as "admin". So the redirect will get the first route with trailing slahes. The only thing now is how can i append extension to the url like .html or .json.



16.3k

Hi, glad to know you solved the problem.

but i just think

$router->add('/admin/:controller/:action/:params', array( 'controller' => 1, 'action' => 2 'param' => 3 ))->setName('admin');

is enough