Me && symfony3 (routing)

Source: Internet
Author: User

Symfony defining routes in the controller

@Route ("/lucky/number") #必须用双引号

@Route ("/api/get-lucky-number/{number}") #{number} is a parameter

ROUTING.YML Configuring routing

API:

path:/API/{_locale}/{year}/{title}.{ _format}

defaults: {_controller: appbundle: Api:getjson, _format: html}

requirements:

_locale: en| Fr

_format: html| RSS

year: \d+

schemes: [http]

Show:

path:/show. JSON

defaults: {_controller: appbundle: api:showurl}

schemes: [http]

TPL:

path:/lucky/ number

defaults: {_controller:appbundle:Api: number}

schemes: [http]

    • API: An alias that represents that reason, using generate && GenerateURL with the controller can be used to
    • Path: Represents access to the URL, where parameters can be passed with {}, passed to the parameters can be used in the controller action parameters
    • Defaults: Indicates the corresponding to the controller and method, Appbundle represents the controller directory, the API represents the controller, Getjson represents the request method
    • Requirements: Represents a parameter to a validation method, where | represents ' or ', \d+ to match a number
    • Schemes: Indicates the corresponding to request mode, HTTP | https

Below is a description of how to use this to router in the controller

# $year, $title is the {} to parameter, $_controller represents the controller, $_route corresponds to the alias Api,show, the TPL

Public function getjsonaction($title, $year, $_locale, $_format, $_ Controller, $_route)

{

s = [];

for ($i = 0; $i < ; $i+ +)

{

s[] = rand (0, +);

}

        $other = [ ' title ' = $title ' year ' = $year ' _locale ' = $_locale ' _format ' = $_ Format ' _controller ' = $_controller = $_route

return New Jsonresponse ($other);

}

/**

#运行结果

{

Title: "API",

Year: "2014",

_locale: "en",

_format: "HTML",

_controller: "Appbundle\controller\apicontroller::getjsonaction",

_route: "API"

}

*/

Public function showurlaction()

{

//Get the corresponding URL

$params = $this->get ('router ')->match ('/lucky/number ');

//Generate URI

$uri = $this->get ('router ')->generate (' API '), [

' year ' = ' 2022 ',

' title ' = ' en ',

]);

//Generate a URL with a domain name

$url = $this->generateurl (' API ', [

' year ' = ' 2022 ',

' title ' = ' fr ',

' _format ' = ' html '

], Urlgeneratorinterface::absolute_url);

$uri = ";

return New Jsonresponse ([' params ' = = $params, ' uri ' = = $uri, ' url ' = = $url]);

}

/**

{

Params: {

_controller: "Appbundle\controller\apicontroller::numberaction",

_route: "App_api_number"

},

URI: "/api/en/2022/en",

URL: "Http://127.0.0.1:8000/api/en/2022/fr"

}

*/

    • $this->get (' router ')->generate (' API ', ...) means getting a URI
    • $this->generateurl (' API ',, Urlgeneratorinterface::absolute_url) is the generation of absolute to URL

Me && symfony3 (routing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.