How to create a friendly url in symfony php?

I am always inclined to forget these built-in Symfony functions for creating links.

+6
source share
4 answers

If your goal is to have user-friendly URLs in your application, use the following approach:

1) Create a routing rule for your module / action in the routing.yml file of the application. The following example is a routing rule for an action that shows the most recent issues in the application, by default on page 1 (using a pager):

recent_questions:
   url:    questions/recent/:page
   param:  { module: questions, action: recent, page: 1 }

2) , url_for() URL-.

<a href="<?php echo url_for('questions/recent?page=1') ?>">Recent Questions</a>

URL: http://myapp/questions/recent/1.html.

3) URL- () , , ( :/page URL-) .

link_to() URL- HTML <a>.

+9

symfony 1.0. , .

sfAction:

string genUrl ($ parameters = array(), $absolute = false)

. $ this- > getController() → genUrl ('yourmodule/youraction? key = value & key2 = value', true);

:

.

string link_to ($ name, $internal_uri, $options = array());

. link_to ('My link name', 'yourmodule/youraction? key = value & key2 = value');

+1

, URL-, :

link_to('My link name', 'yourmodule/youraction?key=value&key2=value',array('query_string'=>'page=2'));

URL- , , .

0

URL , .

URL- , generateUrl():

$this->generateUrl('default', array('module'=>'[ModuleName]','action'=>'[ActionName]'))

URL- , url_for():

url_for('[ModuleName]/[ActionName]', $absolute)

$absolute true/false, , .

But if you want to create a link (something like <a href=""></a>), then link_to () will help .

0
source

All Articles