Symfony.com

Developer
Size
19,712 Kb
Views
6,072

How do I make an symfony.com?

What is a symfony.com? How do you make a symfony.com? This script and codes were developed by Carbon Ads on 12 December 2022, Monday.

Symfony.com Previews

Symfony.com - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>symfony.com</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!DOCTYPE html>
<html lang="en">
<head> <!--[if IE]> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <![endif]--> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta charset="UTF-8" /> <script type="text/javascript"> window.NREUM || (NREUM = {} ), __nr_require = function(e, t, n) { function r(n) { if (!t[n]) { var o = t[n] = { exports: {} }; e[n][0].call(o.exports, function(t) { var o = e[n][1][t]; return r(o || t) }, o, o.exports) } return t[n].exports } if ("function" == typeof __nr_require) return __nr_require; for (var o = 0; o < n.length; o++) r(n[o]); return r } ({ 1: [function(e, t, n) { function r() {} function o(e, t, n) { return function() { return i(e, [(new Date).getTime()].concat(u(arguments)), t ? null : this, n), t ? void 0 : this } } var i = e("handle"), a = e(2), u = e(3), c = e("ee").get("tracer"), f = NREUM; "undefined" == typeof window.newrelic && (newrelic = f); var s = ["setPageViewName", "setCustomAttribute", "setErrorHandler", "finished", "addToTrace", "inlineHit", "addRelease"], l = "api-", p = l + "ixn-"; a(s, function(e, t) { f[t] = o(l + t, !0, "api") }), f.addPageAction = o(l + "addPageAction", !0), f.setCurrentRouteName = o(l + "routeName", !0), t.exports = newrelic, f.interaction = function() { return (new r).get() }; var d = r.prototype = { createTracer: function(e, t) { var n = {}, r = this, o = "function" == typeof t; return i(p + "tracer", [Date.now(), e, n], r), function() { if (c.emit((o ? "" : "no-") + "fn-start", [Date.now(), r, o], n), o) try { return t.apply(this, arguments) } finally { c.emit("fn-end", [Date.now()], n) } } } }; a("setName,setAttribute,save,ignore,onEnd,getContext,end,get".split(","), function(e, t) { d[t] = o(p + t) }), newrelic.noticeError = function(e) { "string" == typeof e && (e = new Error(e)), i("err", [e, (new Date).getTime()]) } }, {}], 2: [function(e, t, n) { function r(e, t) { var n = [], r = "", i = 0; for (r in e) o.call(e, r) && (n[i] = t(r, e[r]), i += 1); return n } var o = Object.prototype.hasOwnProperty; t.exports = r }, {}], 3: [function(e, t, n) { function r(e, t, n) { t || (t = 0), "undefined" == typeof n && (n = e ? e.length : 0); for (var r = -1, o = n - t || 0, i = Array(o < 0 ? 0 : o); ++r < o; ) i[r] = e[t + r]; return i } t.exports = r }, {}], ee: [function(e, t, n) { function r() {} function o(e) { function t(e) { return e && e instanceof r ? e : e ? c(e, u, i) : i() } function n(n, r, o) { if (!p.aborted) { e && e(n, r, o); for (var i = t(o), a = v(n), u = a.length, c = 0; c < u; c++) a[c].apply(i, r); var f = s[w[n]]; return f && f.push([y, n, r, i]), i } } function d(e, t) { b[e] = v(e).concat(t) } function v(e) { return b[e] || [] } function g(e) { return l[e] = l[e] || o(n) } function m(e, t) { f(e, function(e, n) { t = t || "feature", w[n] = t, t in s || (s[t] = []) }) } var b = {}, w = {}, y = { on: d, emit: n, get: g, listeners: v, context: t, buffer: m, abort: a, aborted: !1 }; return y } function i() { return new r } function a() { (s.api || s.feature) && (p.aborted = !0, s = p.backlog = {}) } var u = "nr@context", c = e("gos"), f = e(2), s = {}, l = {}, p = t.exports = o(); p.backlog = s }, {}], gos: [function(e, t, n) { function r(e, t, n) { if (o.call(e, t)) return e[t]; var r = n(); if (Object.defineProperty && Object.keys) try { return Object.defineProperty(e, t, { value: r, writable: !0, enumerable: !1 }), r } catch (i) {} return e[t] = r, r } var o = Object.prototype.hasOwnProperty; t.exports = r }, {}], handle: [function(e, t, n) { function r(e, t, n, r) { o.buffer([e], r), o.emit(e, t, n) } var o = e("ee").get("handle"); t.exports = r, r.ee = o }, {}], id: [function(e, t, n) { function r(e) { var t = typeof e; return !e || "object" !== t && "function" !== t ? -1 : e === window ? 0 : a(e, i, function() { return o++ }) } var o = 1, i = "nr@id", a = e("gos"); t.exports = r }, {}], loader: [function(e, t, n) { function r() { if (!h++) { var e = y.info = NREUM.info, t = l.getElementsByTagName("script")[0]; if (setTimeout(f.abort, 3e4), !(e && e.licenseKey && e.applicationID && t)) return f.abort(); c(b, function(t, n) { e[t] || (e[t] = n) }), u("mark", ["onload", a()], null, "api"); var n = l.createElement("script"); n.src = "https://" + e.agent, t.parentNode.insertBefore(n, t) } } function o() { "complete" === l.readyState && i() } function i() { u("mark", ["domContent", a()], null, "api") } function a() { return (new Date).getTime() } var u = e("handle"), c = e(2), f = e("ee"), s = window, l = s.document, p = "addEventListener", d = "attachEvent", v = s.XMLHttpRequest, g = v && v.prototype; NREUM.o = { ST: setTimeout, CT: clearTimeout, XHR: v, REQ: s.Request, EV: s.Event, PR: s.Promise, MO: s.MutationObserver }, e(1); var m = "" + location, b = { beacon: "bam.nr-data.net", errorBeacon: "bam.nr-data.net", agent: "js-agent.newrelic.com/nr-1016.min.js" }, w = v && g && g[p] && !/CriOS/.test(navigator.userAgent), y = t.exports = { offset: a(), origin: m, features: {}, xhrWrappable: w }; l[p] ? (l[p]("DOMContentLoaded", i, !1), s[p]("load", r, !1)) : (l[d]("onreadystatechange", o), s[d]("onload", r)), u("mark", ["firstbyte", a()], null, "api"); var h = 0 }, {}] } , {} , ["loader"]); </script> <meta name="robots" content="index, follow, all" /> <meta name="Author" content="SensioLabs" /> <meta name="Keywords" content="symfony3, symfony2, symfony, project, framework, php, php5, php7, open-source, components, symphony, symfony framework, symfony tutorial" /> <link rel="search" type="application/opensearchdescription+xml" href="http://symfony.com/opensearch/documentation.xml" title="Symfony2 documentation" /> <title>Controller (current)</title> <link rel="stylesheet" href="http://symfony.com/builds/css/app.css?v=5"> <script src="http://symfony.com/builds/manifest.js?v=5"></script> <script src="http://symfony.com/builds/js/app.js?v=5"></script> <script src="http://symfony.com/builds/js/doc.js?v=5"></script> <link rel="apple-touch-icon" type="image/png" href="/apple-touch-icon.png"> <link rel="apple-touch-icon-precomposed" type="image/png" href="/apple-touch-icon.png"> <meta name="apple-mobile-web-app-title" content="Symfony"> <meta name="twitter:site" content="@symfony"> <meta property="fb:pages" content="211720082183067"> <meta property="og:type" content="website"> <meta property="og:title" content="Controller (current)"> <meta property="og:image" content="http://symfony.com/images/v5/opengraph/symfony_logo_vertical.png?v=5"> <meta name="msapplication-TileColor" content="#000000"> <meta name="application-name" content="Symfony"> <meta name="msapplication-tooltip" content="Official Symfony website"> <meta name="theme-color" content="#222222"> <link rel="canonical" href="/doc/current/controller.html" /> </head>
<body id="" class="doc doc_article" itemscope itemtype="http://schema.org/WebPage"> <div id="sln"></div> <div id="content_wrapper"> <header> <div id="header-top" class="hidden-xs"> <div class="container"> <div class="row"> <div id="header-logo" class="col-sm-5 col-md-6"> <a href="/"> <img src="http://symfony.com/images/v5/logos/header-logo.svg" alt="Symfony" class="img-responsive" /> </a> </div> <div id="header-download" class="col-sm-7 col-md-6"> <a href="https://sensiolabs.com" class="header-sensiolabs-product" title="SensioLabs, PHP services and software solutions for enterprise and community."> <img src="http://symfony.com/images/v5/pictos/slproduct.svg" alt="a SensioLabs product" /> </a> <a href="/download" class="btn btn-primary btn-download">Download</a> </div> </div> </div> </div> <div id="header-bottom" data-spy="affix" data-offset-top="123"> <div class="container"> <div class="visible-xs"> <div id="header-logo-responsive"> <a href="/"> <img src="http://symfony.com/images/v5/logos/sf-white.svg" alt="Symfony" class="img-responsive" /> </a> </div> <a href="" id="menuToggle" class="icon-responsive-menu"></a> </div> <a class="hidden-xs" href="/"> <img id="header-logo-sticky" src="http://symfony.com/images/v5/logos/sf-positive.svg" /> </a> <ul id="header-main-menu" class="row"> <li> <a href="/what-is-symfony"> What is Symfony? </a> </li> <li class="selected"> <a href="/doc/current/index.html"> Documentation </a> </li> <li> <a href="/community"> Community </a> </li> <li> <a href="/showcase"> Showcase </a> </li> <li> <a href="/marketplace"> Marketplace </a> </li> <li> <a href="/jobs"> Jobs </a> </li> <li> <a href="/services"> Business Solutions </a> </li> <li> <a href="/blog/"> News </a> </li> </ul> <div id="header-download-sticky" class="hidden-xs"> <a href="/download">Download</a> </div> </div> </div> </header> <div class="container"> <div id="page-content"> <div class="row"> <div id="sidebar" class="col-sm-3"> <div id="sidebar-content"> <div id="doc-global-nav"> <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=symfonycom" id="_carbonads_js"></script> <div class="panel"> <div class="panel-heading"> <h4><a data-toggle="collapse" href="#doc-menu-1" class="">Getting Started <i class="chevron chevron-right"></i></a></h4> </div> <div id="doc-menu-1" class="panel-collapse collapse in"> <div class="panel-body"> <ul class="unstyled"> <li class=""> <a class="" href="/doc/3.2/setup.html"> Setup </a> </li> <li class=""> <a class="" href="/doc/3.2/page_creation.html"> Creating Pages </a> </li> <li class=""> <a class="" href="/doc/3.2/routing.html"> Routing </a> </li> <li class="selected"> <a class="" href="/doc/3.2/controller.html"> Controllers </a> </li> <li class=""> <a class="" href="/doc/3.2/templating.html"> Templates </a> </li> <li class=""> <a class="" href="/doc/3.2/configuration.html"> Configuration </a> </li> </ul> </div> </div> </div> <div class="panel"> <div class="panel-heading"> <h4><a data-toggle="collapse" href="#doc-menu-2" class="collapsed">Guides <i class="chevron chevron-right"></i></a></h4> </div> <div id="doc-menu-2" class="panel-collapse collapse "> <div class="panel-body"> <ul class=""> <li class=""> <a class="important" href="/doc/3.2/assetic.html"> Assetic </a> </li> <li class=""> <a class="" href="/doc/3.2/bundles.html"> Bundles </a> </li> <li class=""> <a class="important" href="/doc/3.2/console.html"> Console </a> </li> <li class=""> <a class="" href="/doc/3.2/contributing/index.html"> Contributing </a> </li> <li class=""> <a class="very-important" href="/doc/3.2/doctrine.html"> Databases (Doctrine ORM) </a> </li> <li class=""> <a class="" href="/doc/3.2/debug.html"> Debug </a> </li> <li class=""> <a class="" href="/doc/3.2/deployment.html"> Deployment </a> </li> <li class=""> <a class="important" href="/doc/3.2/email.html"> Email </a> </li> <li class=""> <a class="" href="/doc/3.2/event_dispatcher.html"> Event Dispatcher </a> </li> <li class=""> <a class="" href="/doc/3.2/expressions.html"> Expressions </a> </li> <li class=""> <a class="very-important" href="/doc/3.2/forms.html"> Forms </a> </li> <li class=""> <a class="" href="/doc/3.2/frontend.html"> Front-end </a> </li> <li class=""> <a class="" href="/doc/3.2/http_cache.html"> HTTP Cache </a> </li> <li class=""> <a class="" href="/doc/3.2/logging.html"> Logging </a> </li> <li class=""> <a class="" href="/doc/3.2/performance.html"> Performance </a> </li> <li class=""> <a class="" href="/doc/3.2/profiler.html"> Profiler </a> </li> <li class=""> <a class="" href="/doc/3.2/request.html"> Request </a> </li> <li class=""> <a class="important" href="/doc/3.2/security.html"> Security </a> </li> <li class=""> <a class="" href="/doc/3.2/serializer.html"> Serializer </a> </li> <li class=""> <a class="very-important" href="/doc/3.2/service_container.html"> Service Container </a> </li> <li class=""> <a class="" href="/doc/3.2/session.html"> Sessions </a> </li> <li class=""> <a class="" href="/doc/3.2/testing.html"> Testing </a> </li> <li class=""> <a class="" href="/doc/3.2/translation.html"> Translation (i18n) </a> </li> <li class=""> <a class="" href="/doc/3.2/validation.html"> Validation </a> </li> <li class=""> <a class="" href="/doc/3.2/workflow.html"> Workflow </a> </li> </ul> </div> </div> </div> <div class="panel"> <div class="panel-heading"> <h4><a data-toggle="collapse" href="#doc-menu-3" class="collapsed">Components <i class="chevron chevron-right"></i></a></h4> </div> <div id="doc-menu-3" class="panel-collapse collapse "> <div class="panel-body"> <ul class=""> <li class=""> <a class="" href="/doc/3.2/components/asset.html"> Asset </a> </li> <li class=""> <a class="" href="/doc/3.2/components/browser_kit.html"> BrowserKit </a> </li> <li class=""> <a class="" href="/doc/3.2/components/cache.html"> Cache </a> </li> <li class=""> <a class="" href="/doc/3.2/components/class_loader.html"> ClassLoader </a> </li> <li class=""> <a class="" href="/doc/3.2/components/config.html"> Config </a> </li> <li class=""> <a class="" href="/doc/3.2/components/console.html"> Console </a> </li> <li class=""> <a class="" href="/doc/3.2/components/css_selector.html"> CssSelector </a> </li> <li class=""> <a class="" href="/doc/3.2/components/debug.html"> Debug </a> </li> <li class=""> <a class="" href="/doc/3.2/components/dependency_injection.html"> DependencyInjection </a> </li> <li class=""> <a class="" href="/doc/3.2/components/dotenv.html"> Dotenv </a> </li> <li class=""> <a class="" href="/doc/3.2/components/dom_crawler.html"> DomCrawler </a> </li> <li class=""> <a class="" href="/doc/3.2/components/event_dispatcher.html"> EventDispatcher </a> </li> <li class=""> <a class="" href="/doc/3.2/components/expression_language.html"> ExpressionLanguage </a> </li> <li class=""> <a class="" href="/doc/3.2/components/filesystem.html"> Filesystem </a> </li> <li class=""> <a class="" href="/doc/3.2/components/finder.html"> Finder </a> </li> <li class=""> <a class="" href="/doc/3.2/components/form.html"> Form </a> </li> <li class=""> <a class="" href="/doc/3.2/components/http_foundation.html"> HttpFoundation </a> </li> <li class=""> <a class="" href="/doc/3.2/components/http_kernel.html"> HttpKernel </a> </li> <li class=""> <a class="" href="/doc/3.2/components/intl.html"> Intl </a> </li> <li class=""> <a class="" href="/doc/3.2/components/ldap.html"> Ldap </a> </li> <li class=""> <a class="" href="/doc/3.2/components/options_resolver.html"> OptionsResolver </a> </li> <li class=""> <a class="" href="/doc/3.2/components/process.html"> Process </a> </li> <li class=""> <a class="" href="/doc/3.2/components/property_access.html"> PropertyAccess </a> </li> <li class=""> <a class="" href="/doc/3.2/components/property_info.html"> PropertyInfo </a> </li> <li class=""> <a class="" href="/doc/3.2/components/routing.html"> Routing </a> </li> <li class=""> <a class="" href="/doc/3.2/components/security.html"> Security </a> </li> <li class=""> <a class="" href="/doc/3.2/components/serializer.html"> Serializer </a> </li> <li class=""> <a class="" href="/doc/3.2/components/stopwatch.html"> Stopwatch </a> </li> <li class=""> <a class="" href="/doc/3.2/components/templating.html"> Templating </a> </li> <li class=""> <a class="" href="/doc/3.2/components/translation.html"> Translation </a> </li> <li class=""> <a class="" href="/doc/3.2/components/validator.html"> Validator </a> </li> <li class=""> <a class="" href="/doc/3.2/components/var_dumper.html"> VarDumper </a> </li> <li class=""> <a class="" href="/doc/3.2/components/workflow.html"> Workflow </a> </li> <li class=""> <a class="" href="/doc/3.2/components/yaml.html"> Yaml </a> </li> <li class=""> <a class="" href="/doc/3.2/components/phpunit_bridge.html"> PHPUnit Bridge </a> </li> </ul> </div> </div> </div> <div class="panel"> <div class="panel-heading"> <h4><a href="https://training.sensiolabs.com/en/courses?q=symfony">Training</a></h4> </div> </div> <div class="panel"> <div class="panel-heading"> <h4><a href="https://sensiolabs.com/certification">Certification</a></h4> </div> </div> </div> <div class="toc"> <h4>Table of Contents</h4> <div class="toc-content"> <ul> <li><a class="reference internal" href="#">Controller</a> <ul> <li><a class="reference internal" href="#a-simple-controller">A Simple Controller</a> <ul> <li><a class="reference internal" href="#mapping-a-url-to-a-controller">Mapping a URL to a Controller</a></li> </ul> </li> <li><a class="reference internal" href="#the-base-controller-class-services">The Base Controller Class &amp;
Services</a> <ul> <li><a class="reference internal" href="#generating-urls">Generating URLs</a></li> <li><a class="reference internal" href="#redirecting">Redirecting</a></li> <li><a class="reference internal" href="#rendering-templates">Rendering Templates</a></li> <li><a class="reference internal" href="#accessing-other-services">Accessing other Services</a></li> </ul> </li> <li><a class="reference internal" href="#managing-errors-and-404-pages">Managing Errors and 404 Pages</a></li> <li><a class="reference internal" href="#the-request-object-as-a-controller-argument">The Request object as a Controller Argument</a></li> <li><a class="reference internal" href="#managing-the-session">Managing the Session</a> <ul> <li><a class="reference internal" href="#flash-messages">Flash Messages</a></li> </ul> </li> <li><a class="reference internal" href="#the-request-and-response-object">The Request and Response Object</a> <ul> <li><a class="reference internal" href="#json-helper">JSON Helper</a></li> <li><a class="reference internal" href="#file-helper">File helper</a></li> </ul> </li> <li><a class="reference internal" href="#final-thoughts">Final Thoughts</a></li> <li><a class="reference internal" href="#keep-going">Keep Going!</a></li> <li><a class="reference internal" href="#learn-more-about-controllers">Learn more about Controllers</a> <ul> </ul> </li> </ul> </li> </ul> </div> </div> <div class="ads"> <div id="ad_jobs" data-url="http://jobs.sensiolabs.com/api/random"></div> <div class="ad" id="ad_trainings_1"> <h3> <a href="https://training.sensiolabs.com/en/courses?q=symfony" data-tracked data-category="Ads" data-action="Ads" data-label="trainings_1"> Master Symfony fundamentals </a> </h3> <div>Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).</div> <div> <cite>training.sensiolabs.com</cite> </div> </div> <div class="ad" id="ad_support_1"> <h3> <a href="https://sensiolabs.com/en/packaged-solutions/index.html" data-tracked data-category="Ads" data-action="Ads" data-label="support_1"> Discover SensioLabs&#039;
Professional Business Solutions </a> </h3> <div>Peruse our complete Symfony &amp; PHP solutions catalog for your web development needs.</div> <div> <cite>sensiolabs.com</cite> </div> </div> </div> <div class="ads"> <a href="https://blackfire.io/24-days" data-tracked data-category="Ads" data-action="sidebar" data-label="blackfire"><img src="http://symfony.com/images/blackfire-profiler.png?v=5" class="img-responsive" alt="Blackfire Profiler Fire up your PHP Apps Performance" /></a> <a href="https://insight.sensiolabs.com" data-tracked data-category="Ads" data-action="sidebar" data-label="insight"><img src="http://symfony.com/images/sensiolabsinsight.png?v=5" class="img-responsive" alt="PHP Project Quality Done Right" /></a> </div> </div> </div> <div id="main" class="col-sm-9"> <ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"> <li class="" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"> <a itemprop="item" href="/"><span itemprop="name">Home</span></a> <meta itemprop="position" content="1" /> </li> <li class="" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"> <a itemprop="item" href="/doc/current/index.html"><span itemprop="name">Documentation</span></a> <meta itemprop="position" content="2" /> </li> <li class="active" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"> <a itemprop="item" href=""><span itemprop="name">Controller</span></a> <meta itemprop="position" content="3" /> </li> </ol> <h1 class="content_title"> Controller </h1> <div class="page"> <div id="doc-content-tools"> <div class="doc-switcher"> <div class="doc-switcher-wrapper"> <div class="version"> 3.2 version <i class="arrow-down"></i> </div> <div class="all-versions"> <strong class="title">Maintained</strong> <ul> <li> <a href="/doc/2.7/controller.html"> 2.7 </a> </li> <li> <a href="/doc/2.8/controller.html"> 2.8 </a> </li> <li> <a href="/doc/3.2/controller.html"> 3.2 / current </a> </li> <li> <a href="/doc/master/controller.html"> 3.3 / master </a> </li> </ul> <strong class="title">Unmaintained</strong> <ul> <li> <a href="/doc/3.0/controller.html"> 3.0 </a> </li> <li> <a href="/doc/3.1/controller.html"> 3.1 </a> </li> </ul> </div> </div> </div> <div id="doc-edit"> <a href="https://github.com/symfony/symfony-docs/edit/3.2/controller.rst">edit this page</a> </div> </div> <div class="section" id="controller"> <span id="index-0"></span> <h1>Controller<a class="headerlink" href="#controller" title="Permalink to this headline">¶</a></h1> <p>A controller is a PHP function you create that reads information from the Symfony's <code class="notranslate">Request</code> object and creates and returns a <code class="notranslate">Response</code> object. The response could be an HTML page, JSON, XML, a file download, a redirect, a 404 error or anything else you can dream up. The controller executes whatever arbitrary logic <em>your application</em> needs to render the content of a page.</p> <p>See how simple this is by looking at a Symfony controller in action. This renders a page that prints a lucky (random) number:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c1"> // src/AppBundle/Controller/LuckyController.php</span>
<span class="k">namespace</span> <span class="nx">AppBundle\Controller</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Sensio\Bundle\FrameworkExtraBundle\Configuration\Route</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Response</span><span class="p">;
</span> <span class="k">class</span> <span class="nc">LuckyController</span> <span class="p"> { </span> <span class="sd">/**</span>
<span class="sd"> * @Route(&quot;/lucky/number&quot;)</span>
<span class="sd"> */ </span> <span class="k">public</span> <span class="k">function</span> <span class="nf">numberAction</span><span class="p">()</span> <span class="p"> { </span> <span class="nv">$number</span> <span class="o">=</span> <span class="nb">mt_rand</span><span class="p">(</span><span class="mi">0</span><span class="p">, </span> <span class="mi">100</span><span class="p">); </span> <span class="k">return</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span> <span class="s1">&#39; &lt; html&gt; &lt; body&gt; Lucky number: &#39; </span><span class="o">.</span><span class="nv">$number</span><span class="o">.</span><span class="s1">&#39; &lt; /body&gt; &lt; /html&gt; &#39; </span> <span class="p">); </span> <span class="p"> } </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>But in the real world, your controller will probably do a lot of work in order to create the response. It might read information from the request, load a database resource, send an email or set information on the user's session. But in all cases, the controller will eventually return the <code class="notranslate">Response</code> object that will be delivered back to the client.</p> <div class="admonition-wrapper"> <div class="tip"></div> <div class="admonition admonition-tip"> <p class="first admonition-title">Tip</p> <p class="last">If you haven't already created your first working page, check out <a class="reference internal" href="page_creation.html"><em>Create your First Page in Symfony</em></a> and then come back!</p> </div> </div> <div class="section" id="a-simple-controller"> <span id="index-1"></span> <h2>A Simple Controller<a class="headerlink" href="#a-simple-controller" title="Permalink to this headline">¶</a></h2> <p>While a controller can be any PHP callable (a function, method on an object, or a <code class="notranslate">Closure</code>), a controller is usually a method inside a controller class:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c1"> // src/AppBundle/Controller/LuckyController.php</span>
<span class="k">namespace</span> <span class="nx">AppBundle\Controller</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Response</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Sensio\Bundle\FrameworkExtraBundle\Configuration\Route</span><span class="p">;
</span> <span class="k">class</span> <span class="nc">LuckyController</span> <span class="p"> { </span> <span class="sd">/**</span>
<span class="sd"> * @Route(&quot;/lucky/number/{max}&quot;)</span>
<span class="sd"> */ </span> <span class="k">public</span> <span class="k">function</span> <span class="nf">numberAction</span><span class="p">(</span><span class="nv">$max</span><span class="p">)</span> <span class="p"> { </span> <span class="nv">$number</span> <span class="o">=</span> <span class="nb">mt_rand</span><span class="p">(</span><span class="mi">0</span><span class="p">, </span> <span class="nv">$max</span><span class="p">); </span> <span class="k">return</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span> <span class="s1">&#39; &lt; html&gt; &lt; body&gt; Lucky number: &#39; </span><span class="o">.</span><span class="nv">$number</span><span class="o">.</span><span class="s1">&#39; &lt; /body&gt; &lt; /html&gt; &#39; </span> <span class="p">); </span> <span class="p"> } </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>The controller is the <code class="notranslate">numberAction()</code> method, which lives inside a controller class <code class="notranslate">LuckyController</code>.</p> <p>This controller is pretty straightforward:</p> <ul class="simple"> <li><em>line 2</em>: Symfony takes advantage of PHP's namespace functionality to namespace the entire controller class.</li> <li><em>line 4</em>: Symfony again takes advantage of PHP's namespace functionality: the <code class="notranslate">use</code> keyword imports the <code class="notranslate">Response</code> class, which the controller must return.</li> <li><em>line 7</em>: The class can technically be called anything - but should end in the word <code class="notranslate">Controller</code> (this isn't <em>required</em>, but some shortcuts rely on this).</li> <li><em>line 12</em>: Each action method in a controller class is suffixed with <code class="notranslate">Action</code> (again, this isn't <em>required</em>, but some shortcuts rely on this). This method is allowed to have a <code class="notranslate">$max</code> argument thanks to the <code class="notranslate"> { max
}
</code> <a class="reference internal" href="routing.html"><em>wildcard in the route</em></a>.</li> <li><em>line 16</em>: The controller creates and returns a <code class="notranslate">Response</code> object.</li> </ul> <div class="section" id="mapping-a-url-to-a-controller"> <span id="index-2"></span> <h3>Mapping a URL to a Controller<a class="headerlink" href="#mapping-a-url-to-a-controller" title="Permalink to this headline">¶</a></h3> <p>In order to <em>view</em> the result of this controller, you need to map a URL to it via a route. This was done above with the <code class="notranslate">&#64;
Route(&quot;
/lucky/number/ { max
}
&quot;
)</code> annotation.</p> <p>To see your page, go to this URL in your browser:</p> <blockquote> <div><a class="reference external" href="http://localhost:8000/lucky/number/100">http: //localhost:8000/lucky/number/100</a></div> </blockquote> <p>For more information on routing, see <a class="reference internal" href="routing.html"><em>Routing</em></a>.</p> </div> </div> <div class="section" id="the-base-controller-class-services"> <span id="index-3"></span> <h2>The Base Controller Class &amp;
Services<a class="headerlink" href="#the-base-controller-class-services" title="Permalink to this headline">¶</a></h2> <p>For convenience, Symfony comes with an optional base <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html" title="Symfony\Bundle\FrameworkBundle\Controller\Controller">Controller</a></code> class. If you extend it, this won't change anything about how your controller works, but you'll get access to a number of <strong>helper methods</strong> and the <strong>service container</strong> (see <a class="reference internal" href="#controller-accessing-services"><span>Accessing other Services</span></a>): an array-like object that gives you access to every useful object in the system. These useful objects are called <strong>services</strong>, and Symfony ships with a service object that can render Twig templates, another that can log messages and many more.</p> <p>Add the <code class="notranslate">use</code> statement atop the <code class="notranslate">Controller</code> class and then modify <code class="notranslate">LuckyController</code> to extend it:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5 6 7 8 9</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c1"> // src/AppBundle/Controller/LuckyController.php</span>
<span class="k">namespace</span> <span class="nx">AppBundle\Controller</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Symfony\Bundle\FrameworkBundle\Controller\Controller</span><span class="p">;
</span> <span class="k">class</span> <span class="nc">LuckyController</span> <span class="k">extends</span> <span class="nx">Controller</span> <span class="p"> { </span> <span class="c1"> // ...</span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>Helper methods are just shortcuts to using core Symfony functionality that's available to you with or without the use of the base <code class="notranslate">Controller</code> class. A great way to see the core functionality in action is to look in the <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html" title="Symfony\Bundle\FrameworkBundle\Controller\Controller">Controller</a></code> class.</p> <div class="section" id="generating-urls"> <span id="index-4"></span> <h3>Generating URLs<a class="headerlink" href="#generating-urls" title="Permalink to this headline">¶</a></h3> <p>The <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_generateUrl" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::generateUrl()">generateUrl()</a></code> method is just a helper method that generates the URL for a given route:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <div class="highlight"><pre><span class="nv">$url</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">generateUrl</span><span class="p">(</span><span class="s1">&#39;
blog_show&#39;
</span><span class="p">, </span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;
slug&#39;
</span> <span class="o">=&gt;
</span> <span class="s1">&#39;
slug-value&#39;
</span><span class="p">));
</span> </pre></div> </div> </div> </div> <div class="section" id="redirecting"> <h3>Redirecting<a class="headerlink" href="#redirecting" title="Permalink to this headline">¶</a></h3> <p>If you want to redirect the user to another page, use the <code class="notranslate">redirectToRoute()</code> and <code class="notranslate">redirect()</code> methods:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span> <span class="p"> { </span> <span class="c1"> // redirect to the &quot;homepage&quot; route</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">redirectToRoute</span><span class="p">(</span><span class="s1">&#39; homepage&#39; </span><span class="p">); </span> <span class="c1"> // do a permanent - 301 redirect</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">redirectToRoute</span><span class="p">(</span><span class="s1">&#39; homepage&#39; </span><span class="p">, </span> <span class="k">array</span><span class="p">(), </span> <span class="mi">301</span><span class="p">); </span> <span class="c1"> // redirect to a route with parameters</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">redirectToRoute</span><span class="p">(</span><span class="s1">&#39; blog_show&#39; </span><span class="p">, </span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39; slug&#39; </span> <span class="o">=&gt; </span> <span class="s1">&#39; my-page&#39; </span><span class="p">)); </span> <span class="c1"> // redirect externally</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">redirect</span><span class="p">(</span><span class="s1">&#39; http: //symfony.com/doc&#39;</span><span class="p">);</span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>For more information, see the <a class="reference internal" href="routing.html"><em>Routing article</em></a>.</p> <div class="admonition-wrapper"> <div class="caution"></div> <div class="admonition admonition-caution"> <p class="first admonition-title">Caution</p> <p class="last">The <code class="notranslate">redirect()</code> method does not check its destination in any way. If you redirect to some URL provided by the end-users, your application may be open to the <a class="reference external" href="https://www.owasp.org/index.php/Open_redirect">unvalidated redirects security vulnerability</a>.</p> </div> </div> <div class="admonition-wrapper"> <div class="tip"></div> <div class="admonition admonition-tip"> <p class="first admonition-title">Tip</p> <p>The <code class="notranslate">redirectToRoute()</code> method is simply a shortcut that creates a <code class="notranslate">Response</code> object that specializes in redirecting the user. It's equivalent to:</p> <div class="literal-block notranslate"> <div class="last highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5 6</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\RedirectResponse</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span> <span class="p"> { </span> <span class="k">return</span> <span class="k">new</span> <span class="nx">RedirectResponse</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt; </span><span class="na">generateUrl</span><span class="p">(</span><span class="s1">&#39; homepage&#39; </span><span class="p">)); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> </div> </div> </div> <div class="section" id="rendering-templates"> <span id="controller-rendering-templates"></span><span id="index-5"></span> <h3>Rendering Templates<a class="headerlink" href="#rendering-templates" title="Permalink to this headline">¶</a></h3> <p>If you're serving HTML, you'll want to render a template. The <code class="notranslate">render()</code> method renders a template <strong>and</strong> puts that content into a <code class="notranslate">Response</code> object for you:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <div class="highlight"><pre><span class="c1"> // renders app/Resources/views/lucky/number.html.twig</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;
lucky/number.html.twig&#39;
</span><span class="p">, </span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;
name&#39;
</span> <span class="o">=&gt;
</span> <span class="nv">$name</span><span class="p">));
</span> </pre></div> </div> </div> <p>Templates can also live in deeper sub-directories. Just try to avoid creating unnecessarily deep structures:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <div class="highlight"><pre><span class="c1"> // renders app/Resources/views/lottery/lucky/number.html.twig</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;
lottery/lucky/number.html.twig&#39;
</span><span class="p">, </span> <span class="k">array</span><span class="p">(</span> <span class="s1">&#39;
name&#39;
</span> <span class="o">=&gt;
</span> <span class="nv">$name</span><span class="p">, </span> <span class="p">));
</span> </pre></div> </div> </div> <p>The Symfony templating system and Twig are explained more in the <a class="reference internal" href="templating.html"><em>Creating and Using Templates article</em></a>.</p> </div> <div class="section" id="accessing-other-services"> <span id="controller-accessing-services"></span><span id="index-6"></span> <h3>Accessing other Services<a class="headerlink" href="#accessing-other-services" title="Permalink to this headline">¶</a></h3> <p>Symfony comes packed with a lot of useful objects, called <em>services</em>. These are used for rendering templates, sending emails, querying the database and any other &quot; work&quot; you can think of. When you install a new bundle, it probably brings in even <em>more</em> services.</p> <p>When extending the base controller class, you can access any Symfony service via the <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_get" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::get()">get()</a></code> method of the <code class="notranslate">Controller</code> class. Here are several common services you might need:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="nv">$templating</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;
templating&#39;
</span><span class="p">);
</span> <span class="nv">$router</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;
router&#39;
</span><span class="p">);
</span> <span class="nv">$mailer</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;
mailer&#39;
</span><span class="p">);
</span> </pre></div> </td> </tr> </table> </div> </div> <p>What other services exist? To list all services, use the <code class="notranslate">debug:container</code> console command:</p> <div class="literal-block notranslate"> <div class="highlight-terminal"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="gp">$</span> php bin/console debug:container </pre></div> </td> </tr> </table> </div> </div> <p>For more information, see the <a class="reference internal" href="service_container.html"><em>Service Container</em></a> article.</p> <div class="admonition-wrapper"> <div class="tip"></div> <div class="admonition admonition-tip"> <p class="first admonition-title">Tip</p> <p>To get a <a class="reference internal" href="configuration.html#config-parameter-intro"><span>container configuration parameter</span></a>, use the <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_getParameter" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::getParameter()">getParameter()</a></code> method:</p> <div class="literal-block notranslate"> <div class="last highlight-php"> <div class="highlight"><pre><span class="nv">$from</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;
</span><span class="na">getParameter</span><span class="p">(</span><span class="s1">&#39;
app.mailer.from&#39;
</span><span class="p">);
</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="section" id="managing-errors-and-404-pages"> <span id="index-7"></span> <h2>Managing Errors and 404 Pages<a class="headerlink" href="#managing-errors-and-404-pages" title="Permalink to this headline">¶</a></h2> <p>When things are not found, you should play well with the HTTP protocol and return a 404 response. To do this, you'll throw a special type of exception. If you're extending the base <code class="notranslate">Controller</code> class, do the following:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span> <span class="p"> { </span> <span class="c1"> // retrieve the object from database</span> <span class="nv">$product</span> <span class="o">=</span> <span class="o">...</span><span class="p">; </span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$product</span><span class="p">)</span> <span class="p"> { </span> <span class="k">throw</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">createNotFoundException</span><span class="p">(</span><span class="s1">&#39; The product does not exist&#39; </span><span class="p">); </span> <span class="p"> } </span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">render</span><span class="p">(</span><span class="o">...</span><span class="p">); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>The <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_createNotFoundException" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::createNotFoundException()">createNotFoundException()</a></code> method is just a shortcut to create a special <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpKernel/Exception/NotFoundHttpException.html" title="Symfony\Component\HttpKernel\Exception\NotFoundHttpException">NotFoundHttpException</a></code> object, which ultimately triggers a 404 HTTP response inside Symfony.</p> <p>Of course, you're free to throw any <code class="notranslate">Exception</code> class in your controller - Symfony will automatically return a 500 HTTP response code.</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">throw</span> <span class="k">new</span> <span class="nx">\Exception</span><span class="p">(</span><span class="s1">&#39;
Something went wrong!&#39;
</span><span class="p">);
</span> </pre></div> </td> </tr> </table> </div> </div> <p>In every case, an error page is shown to the end user and a full debug error page is shown to the developer (i.e. when you're using the <code class="notranslate">app_dev.php</code> front controller - see <a class="reference internal" href="configuration.html#page-creation-environments"><span>The imports Key: Loading other Configuration Files</span></a>).</p> <p>You'll want to customize the error page your user sees. To do that, see the <a class="reference internal" href="controller/error_pages.html"><em>How to Customize Error Pages</em></a> article.</p> </div> <div class="section" id="the-request-object-as-a-controller-argument"> <span id="controller-request-argument"></span><span id="index-8"></span> <h2>The Request object as a Controller Argument<a class="headerlink" href="#the-request-object-as-a-controller-argument" title="Permalink to this headline">¶</a></h2> <p>What if you need to read query parameters, grab a request header or get access to an uploaded file? All of that information is stored in Symfony's <code class="notranslate">Request</code> object. To get it in your controller, just add it as an argument and <strong>type-hint it with the Request class</strong>:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5 6 7 8</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Request</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">(</span><span class="nx">Request</span> <span class="nv">$request</span><span class="p">, </span> <span class="nv">$firstName</span><span class="p">, </span> <span class="nv">$lastName</span><span class="p">)</span> <span class="p"> { </span> <span class="nv">$page</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">query</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; page&#39; </span><span class="p">, </span> <span class="mi">1</span><span class="p">); </span> <span class="c1"> // ...</span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p><a class="reference internal" href="#request-object-info"><span>Keep reading</span></a> for more information about using the Request object.</p> </div> <div class="section" id="managing-the-session"> <h2>Managing the Session<a class="headerlink" href="#managing-the-session" title="Permalink to this headline">¶</a></h2> <p>Symfony provides a nice session object that you can use to store information about the user between requests. By default, Symfony stores the attributes in a cookie by using native PHP sessions.</p> <p>To retrieve the session, call <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_getSession" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::getSession()">getSession()</a></code> method on the <code class="notranslate">Request</code> object. This method returns a <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/Session/SessionInterface.html" title="Symfony\Component\HttpFoundation\Session\SessionInterface">SessionInterface</a></code> with easy methods for storing and fetching things from the session:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Request</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">(</span><span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span> <span class="p"> { </span> <span class="nv">$session</span> <span class="o">=</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">getSession</span><span class="p">(); </span> <span class="c1"> // store an attribute for reuse during a later user request</span> <span class="nv">$session</span><span class="o">-&gt; </span><span class="na">set</span><span class="p">(</span><span class="s1">&#39; foo&#39; </span><span class="p">, </span> <span class="s1">&#39; bar&#39; </span><span class="p">); </span> <span class="c1"> // get the attribute set by another controller in another request</span> <span class="nv">$foobar</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; foobar&#39; </span><span class="p">); </span> <span class="c1"> // use a default value if the attribute doesn&#39;t exist</span> <span class="nv">$filters</span> <span class="o">=</span> <span class="nv">$session</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; filters&#39; </span><span class="p">, </span> <span class="k">array</span><span class="p">()); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>Stored attributes remain in the session for the remainder of that user's session.</p> <div class="section" id="flash-messages"> <span id="index-9"></span> <h3>Flash Messages<a class="headerlink" href="#flash-messages" title="Permalink to this headline">¶</a></h3> <p>You can also store special messages, called &quot; flash&quot; messages, on the user's session. By design, flash messages are meant to be used exactly once: they vanish from the session automatically as soon as you retrieve them. This feature makes &quot; flash&quot; messages particularly great for storing user notifications.</p> <p>For example, imagine you're processing a <a class="reference internal" href="forms.html"><em>form</em></a> submission:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Request</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">updateAction</span><span class="p">(</span><span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span> <span class="p"> { </span> <span class="c1"> // ...</span> <span class="k">if</span> <span class="p">(</span><span class="nv">$form</span><span class="o">-&gt; </span><span class="na">isSubmitted</span><span class="p">()</span> <span class="o">&amp; &amp; </span> <span class="nv">$form</span><span class="o">-&gt; </span><span class="na">isValid</span><span class="p">())</span> <span class="p"> { </span> <span class="c1"> // do some sort of processing</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">addFlash</span><span class="p">(</span> <span class="s1">&#39; notice&#39; </span><span class="p">, </span> <span class="s1">&#39; Your changes were saved!&#39; </span> <span class="p">); </span> <span class="c1"> // $this-&gt;addFlash() is equivalent to $request-&gt;getSession()-&gt;getFlashBag()-&gt;add()</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">redirectToRoute</span><span class="p">(</span><span class="o">...</span><span class="p">); </span> <span class="p"> } </span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">render</span><span class="p">(</span><span class="o">...</span><span class="p">); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>After processing the request, the controller sets a flash message in the session and then redirects. The message key (<code class="notranslate">notice</code> in this example) can be anything: you'll use this key to retrieve the message.</p> <p>In the template of the next page (or even better, in your base layout template), read any flash messages from the session:</p> <div class="configuration-block"> <ul class="simple"> <li><em>Twig</em> <div class="literal-block notranslate"> <div class="highlight-html+twig"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c"> { # app/Resources/views/base.html.twig #
}
</span> <span class="c"> { # you can read and display just one flash message type... #
}
</span> <span class="cp"> { %</span> <span class="k">for</span> <span class="nv">flash_message</span> <span class="k">in</span> <span class="nv">app.session.flashBag.get</span><span class="o">(</span><span class="s1">&#39; notice&#39; </span><span class="o">)</span> <span class="cp">%
}
</span> <span class="nt">&lt;
div</span> <span class="na">class=</span><span class="s">&quot;
flash-notice&quot;
</span><span class="nt">&gt;
</span> <span class="cp"> { { </span> <span class="nv">flash_message</span> <span class="cp"> }
}
</span> <span class="nt">&lt;
/div&gt;
</span> <span class="cp"> { %</span> <span class="k">endfor</span> <span class="cp">%
}
</span> <span class="c"> { # ...or you can read and display every flash message available #
}
</span> <span class="cp"> { %</span> <span class="k">for</span> <span class="nv">type</span><span class="o">, </span> <span class="nv">flash_messages</span> <span class="k">in</span> <span class="nv">app.session.flashBag.all</span> <span class="cp">%
}
</span> <span class="cp"> { %</span> <span class="k">for</span> <span class="nv">flash_message</span> <span class="k">in</span> <span class="nv">flash_messages</span> <span class="cp">%
}
</span> <span class="nt">&lt;
div</span> <span class="na">class=</span><span class="s">&quot;
flash-</span><span class="cp"> { { </span> <span class="nv">type</span> <span class="cp"> }
}
</span><span class="s">&quot;
</span><span class="nt">&gt;
</span> <span class="cp"> { { </span> <span class="nv">flash_message</span> <span class="cp"> }
}
</span> <span class="nt">&lt;
/div&gt;
</span> <span class="cp"> { %</span> <span class="k">endfor</span> <span class="cp">%
}
</span> <span class="cp"> { %</span> <span class="k">endfor</span> <span class="cp">%
}
</span> </pre></div> </td> </tr> </table> </div> </div> </li> <li><em>PHP</em> <div class="literal-block notranslate"> <div class="highlight-html+php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c">&lt;
!-- app/Resources/views/base.html.php --&gt;
</span> // you can read and display just one flash message type...
<span class="cp">&lt;
?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$view</span><span class="p">[</span><span class="s1">&#39;
session&#39;
</span><span class="p">]</span><span class="o">-&gt;
</span><span class="na">getFlashBag</span><span class="p">()</span><span class="o">-&gt;
</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;
notice&#39;
</span><span class="p">)</span> <span class="k">as</span> <span class="nv">$message</span><span class="p">)</span><span class="o">:</span> <span class="cp">?&gt;
</span> <span class="nt">&lt;
div</span> <span class="na">class=</span><span class="s">&quot;
flash-notice&quot;
</span><span class="nt">&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">echo</span> <span class="nv">$message</span> <span class="cp">?&gt;
</span> <span class="nt">&lt;
/div&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">endforeach</span> <span class="cp">?&gt;
</span> // ...or you can read and display every flash message available
<span class="cp">&lt;
?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$view</span><span class="p">[</span><span class="s1">&#39;
session&#39;
</span><span class="p">]</span><span class="o">-&gt;
</span><span class="na">getFlashBag</span><span class="p">()</span><span class="o">-&gt;
</span><span class="na">all</span><span class="p">()</span> <span class="k">as</span> <span class="nv">$type</span> <span class="o">=&gt;
</span> <span class="nv">$flash_messages</span><span class="p">)</span><span class="o">:</span> <span class="cp">?&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$flash_messages</span> <span class="k">as</span> <span class="nv">$flash_message</span><span class="p">)</span><span class="o">:</span> <span class="cp">?&gt;
</span> <span class="nt">&lt;
div</span> <span class="na">class=</span><span class="s">&quot;
flash-</span><span class="cp">&lt;
?php</span> <span class="k">echo</span> <span class="nv">$type</span> <span class="cp">?&gt;
</span><span class="s">&quot;
</span><span class="nt">&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">echo</span> <span class="nv">$message</span> <span class="cp">?&gt;
</span> <span class="nt">&lt;
/div&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">endforeach</span> <span class="cp">?&gt;
</span> <span class="cp">&lt;
?php</span> <span class="k">endforeach</span> <span class="cp">?&gt;
</span> </pre></div> </td> </tr> </table> </div> </div> </li> </ul> </div> <div class="admonition-wrapper"> <div class="note"></div> <div class="admonition admonition-note"> <p class="first admonition-title">Note</p> <p class="last">It's common to use <code class="notranslate">notice</code>, <code class="notranslate">warning</code> and <code class="notranslate">error</code> as the keys of the different types of flash messages, but you can use any key that fits your needs.</p> </div> </div> <div class="admonition-wrapper"> <div class="tip"></div> <div class="admonition admonition-tip"> <p class="first admonition-title">Tip</p> <p class="last">You can use the <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.html#method_peek" title="Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface::peek()">peek()</a></code> method instead to retrieve the message while keeping it in the bag.</p> </div> </div> </div> </div> <div class="section" id="the-request-and-response-object"> <span id="request-object-info"></span><span id="index-10"></span> <h2>The Request and Response Object<a class="headerlink" href="#the-request-and-response-object" title="Permalink to this headline">¶</a></h2> <p>As mentioned <a class="reference internal" href="#controller-request-argument"><span>earlier</span></a>, the framework will pass the <code class="notranslate">Request</code> object to any controller argument that is type-hinted with the <code class="notranslate">Request</code> class:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Request</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">(</span><span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span> <span class="p"> { </span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">isXmlHttpRequest</span><span class="p">(); </span> <span class="c1"> // is it an Ajax request?</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">getPreferredLanguage</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39; en&#39; </span><span class="p">, </span> <span class="s1">&#39; fr&#39; </span><span class="p">)); </span> <span class="c1"> // retrieve GET and POST variables respectively</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">query</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; page&#39; </span><span class="p">); </span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">request</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; page&#39; </span><span class="p">); </span> <span class="c1"> // retrieve SERVER variables</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">server</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; HTTP_HOST&#39; </span><span class="p">); </span> <span class="c1"> // retrieves an instance of UploadedFile identified by foo</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">files</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; foo&#39; </span><span class="p">); </span> <span class="c1"> // retrieve a COOKIE value</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">cookies</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; PHPSESSID&#39; </span><span class="p">); </span> <span class="c1"> // retrieve an HTTP request header, with normalized, lowercase keys</span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">headers</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; host&#39; </span><span class="p">); </span> <span class="nv">$request</span><span class="o">-&gt; </span><span class="na">headers</span><span class="o">-&gt; </span><span class="na">get</span><span class="p">(</span><span class="s1">&#39; content_type&#39; </span><span class="p">); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>The <code class="notranslate">Request</code> class has several public properties and methods that return any information you need about the request.</p> <p>Like the <code class="notranslate">Request</code>, the <code class="notranslate">Response</code> object has also a public <code class="notranslate">headers</code> property. This is a <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/ResponseHeaderBag.html" title="Symfony\Component\HttpFoundation\ResponseHeaderBag">ResponseHeaderBag</a></code> that has some nice methods for getting and setting response headers. The header names are normalized so that using <code class="notranslate">Content-Type</code> is equivalent to <code class="notranslate">content-type</code> or even <code class="notranslate">content_type</code>.</p> <p>The only requirement for a controller is to return a <code class="notranslate">Response</code> object. The <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/Response.html" title="Symfony\Component\HttpFoundation\Response">Response</a></code> class is an abstraction around the HTTP response - the text-based message filled with headers and content that's sent back to the client:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5 6 7 8</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\Response</span><span class="p">;
</span> <span class="c1"> // create a simple Response with a 200 status code (the default)</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span><span class="s1">&#39;
Hello &#39;
</span><span class="o">.</span><span class="nv">$name</span><span class="p">, </span> <span class="nx">Response</span><span class="o">::</span><span class="na">HTTP_OK</span><span class="p">);
</span> <span class="c1"> // create a CSS-response with a 200 status code</span>
<span class="nv">$response</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Response</span><span class="p">(</span><span class="s1">&#39;
&lt;
style&gt;
... &lt;
/style&gt;
&#39;
</span><span class="p">);
</span> <span class="nv">$response</span><span class="o">-&gt;
</span><span class="na">headers</span><span class="o">-&gt;
</span><span class="na">set</span><span class="p">(</span><span class="s1">&#39;
Content-Type&#39;
</span><span class="p">, </span> <span class="s1">&#39;
text/css&#39;
</span><span class="p">);
</span> </pre></div> </td> </tr> </table> </div> </div> <p>There are special classes that make certain kinds of responses easier:</p> <ul class="simple"> <li>For files, there is <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/BinaryFileResponse.html" title="Symfony\Component\HttpFoundation\BinaryFileResponse">BinaryFileResponse</a></code>. See <a class="reference internal" href="components/http_foundation.html#component-http-foundation-serving-files"><span>Serving Files</span></a>.</li> <li>For streamed responses, there is <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Component/HttpFoundation/StreamedResponse.html" title="Symfony\Component\HttpFoundation\StreamedResponse">StreamedResponse</a></code>. See <a class="reference internal" href="components/http_foundation.html#streaming-response"><span>Streaming a Response</span></a>.</li> </ul> <div class="admonition-wrapper"> <div class="seealso"></div> <div class="admonition admonition-seealso">Now that you know the basics you can continue your research on Symfony <code class="notranslate">Request</code> and <code class="notranslate">Response</code> object in the <a class="reference internal" href="components/http_foundation.html#component-http-foundation-request"><span>HttpFoundation component documentation</span></a>.</div> </div> <div class="section" id="json-helper"> <h3>JSON Helper<a class="headerlink" href="#json-helper" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <p><span class="versionnumber">3.1</span>The <code class="notranslate">json()</code> helper was introduced in Symfony 3.1.</p> </div> <p>To return JSON from a controller, use the <code class="notranslate">json()</code> helper method on the base controller. This returns a special <code class="notranslate">JsonResponse</code> object that encodes the data automatically:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5 6 7 8 9</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="c1"> // ...</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span> <span class="p"> { </span> <span class="c1"> // returns &#39;{&quot;username&quot;:&quot;jane.doe&quot;}&#39; and sets the proper Content-Type header</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">json</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39; username&#39; </span> <span class="o">=&gt; </span> <span class="s1">&#39; jane.doe&#39; </span><span class="p">)); </span> <span class="c1"> // the shortcut defines three optional arguments</span> <span class="c1"> // return $this-&gt;json($data, $status = 200, $headers = array(), $context = array());</span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>If the <a class="reference internal" href="serializer.html"><em>serializer service</em></a> is enabled in your application, contents passed to <code class="notranslate">json()</code> are encoded with it. Otherwise, the <code class="notranslate"><a class="reference external" href="http://php.net/manual/en/function.json-encode.php" title="json_encode">json_encode</a></code> function is used.</p> </div> <div class="section" id="file-helper"> <h3>File helper<a class="headerlink" href="#file-helper" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <p><span class="versionmodified">New in version 3.2: </span>The <code class="notranslate">file()</code> helper was introduced in Symfony 3.2.</p> </div> <p>You can use the <code class="notranslate"><a class="reference external" href="http://api.symfony.com/3.2/Symfony/Bundle/FrameworkBundle/Controller/Controller.html#method_file" title="Symfony\Bundle\FrameworkBundle\Controller\Controller::file()">file()</a></code> helper to serve a file from inside a controller:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre>1 2 3 4 5</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">public</span> <span class="k">function</span> <span class="nf">fileAction</span><span class="p">()</span> <span class="p"> { </span> <span class="c1"> // send the file contents and force the browser to download it</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">file</span><span class="p">(</span><span class="s1">&#39; /path/to/some_file.pdf&#39; </span><span class="p">); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> <p>The <code class="notranslate">file()</code> helper provides some arguments to configure its behavior:</p> <div class="literal-block notranslate"> <div class="highlight-php"> <table class="highlighttable"> <tr> <td class="linenos"> <div class="linenodiv"><pre> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16</pre></div> </td> <td class="code"> <div class="highlight"><pre><span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\File\File</span><span class="p">;
</span> <span class="k">use</span> <span class="nx">Symfony\Component\HttpFoundation\ResponseHeaderBag</span><span class="p">;
</span> <span class="k">public</span> <span class="k">function</span> <span class="nf">fileAction</span><span class="p">()</span> <span class="p"> { </span> <span class="c1"> // load the file from the filesystem</span> <span class="nv">$file</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">File</span><span class="p">(</span><span class="s1">&#39; /path/to/some_file.pdf&#39; </span><span class="p">); </span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">file</span><span class="p">(</span><span class="nv">$file</span><span class="p">); </span> <span class="c1"> // rename the downloaded file</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">file</span><span class="p">(</span><span class="nv">$file</span><span class="p">, </span> <span class="s1">&#39; custom_name.pdf&#39; </span><span class="p">); </span> <span class="c1"> // display the file contents in the browser instead of downloading it</span> <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt; </span><span class="na">file</span><span class="p">(</span><span class="s1">&#39; invoice_3241.pdf&#39; </span><span class="p">, </span> <span class="s1">&#39; my_invoice.pdf&#39; </span><span class="p">, </span> <span class="nx">ResponseHeaderBag</span><span class="o">: : </span><span class="na">DISPOSITION_INLINE</span><span class="p">); </span> <span class="p">
}
</span> </pre></div> </td> </tr> </table> </div> </div> </div> </div> <div class="section" id="final-thoughts"> <h2>Final Thoughts<a class="headerlink" href="#final-thoughts" title="Permalink to this headline">¶</a></h2> <p>Whenever you create a page, you'll ultimately need to write some code that contains the logic for that page. In Symfony, this is called a controller, and it's a PHP function where you can do anything in order to return the final <code class="notranslate">Response</code> object that will be returned to the user.</p> <p>To make life easier, you'll probably extend the base <code class="notranslate">Controller</code> class because this gives two things:</p> <ol class="upperalpha simple"> <li>Shortcut methods (like <code class="notranslate">render()</code> and <code class="notranslate">redirectToRoute()</code>); </li> <li>Access to <em>all</em> of the useful objects (services) in the system via the <a class="reference internal" href="#controller-accessing-services"><span>get()</span></a> method.</li> </ol> <p>In other articles, you'll learn how to use specific services from inside your controller that will help you persist and fetch objects from a database, process form submissions, handle caching and more.</p> </div> <div class="section" id="keep-going"> <h2>Keep Going!<a class="headerlink" href="#keep-going" title="Permalink to this headline">¶</a></h2> <p>Next, learn all about <a class="reference internal" href="templating.html"><em>rendering templates with Twig</em></a>.</p> </div> <div class="section" id="learn-more-about-controllers"> <h2>Learn more about Controllers<a class="headerlink" href="#learn-more-about-controllers" title="Permalink to this headline">¶</a></h2> <div class="toctree-wrapper compound"> </div> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="controller/argument_value_resolver.html">Extending Action Argument Resolving</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/csrf_token_validation.html">How to Manually Validate a CSRF Token in a Controller</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/error_pages.html">How to Customize Error Pages</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/forwarding.html">How to Forward Requests to another Controller</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/service.html">How to Define Controllers as Services</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/soap_web_service.html">How to Create a SOAP Web Service in a Symfony Controller</a></li> <li class="toctree-l1"><a class="reference internal" href="controller/upload_file.html">How to Upload Files</a></li> </ul> </div> </div> </div> </div> <ul class="row pager"> <li class="col-xs-6 previous"> <a accesskey="P" title="How to Allow a &amp;quot;/&amp;quot; Character in a Route Parameter" href="routing/slash_in_parameter.html"> &laquo;
How to Allow a &quot;
/&quot;
Character in a Route Parameter </a> </li> <li class="col-xs-6 next"> <a accesskey="N" title="Creating and Using Templates" href="templating.html"> Creating and Using Templates &raquo;
</a> </li> </ul> <div id="license"> <p> This work, including the code samples, is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by-sa/3.0/">Creative Commons BY-SA 3.0</a> license. </p> </div> </div> </div> </div> </div> <footer> <div class="promos"> <div class="container"> <div class="row"> <div class="col-sm-5 col-md-4"> <h2 class="title_05">News from the Symfony blog</h2> <ul class="event_list"> <li> <h3> <a href="/blog/a-week-of-symfony-536-3-9-april-2017"> A week of symfony #536 (3-9 April 2017) <span class="place_date">April 09, 2017</span> </a> </h3> </li> <li> <h3> <a href="/blog/new-in-symfony-3-3-improved-flash-messages"> New in Symfony 3.3: Improved flash messages <span class="place_date">April 07, 2017</span> </a> </h3> </li> <li> <h3> <a href="/blog/new-in-symfony-3-3-workflow-improvements"> New in Symfony 3.3: Workflow improvements <span class="place_date">April 06, 2017</span> </a> </h3> </li> </ul> <div class="read-more link_01"> <a href="/blog/">Visit The Symfony Blog</a> </div> </div> <div class="col-sm-4 col-md-4"> <h2 class="title_05">In the news</h2> <div class="upcoming_event clear_fix"> <div> <a href="https://sensiolabs.com/symfony-certification" target="_blank"> <img style="width: 100px" src="http://symfony.com/images/certification-big.png?v=5" class="png_fix" alt="The Symfony Certification by SensioLabs" /> </a> </div> <div style="margin-top: 13px"> <h3 style="font-size: 13px; font-family: 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif">Symfony 3 Certification now available in 4, 000 centers around the world!</h3> <div style="margin-top: 5px"> <a href="https://sensiolabs.com/en/symfony/certification/order" class="non_extensible_button button_01"> <span class="border_l png_fix"> <span class="border_r png_fix"> <span class="btn_bg png_fix"> GET CERTIFIED </span> </span> </span> </a> </div> </div> </div> </div> <div class="col-sm-3 col-md-4"> <h2>Upcoming training sessions</h2> <ul class="event_list"> <li itemscope itemtype="http://schema.org/Event"> <a itemprop="url" href="http://training.sensiolabs.com/en/courses?q=SF3C2&amp;from=04/12/2017&amp;to=04/13/2017"> <span itemprop="name">Mastering Symfony 3</span> <br /> <span class="metadata">Berlin - 2017-04-12</span> <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <span itemprop="addressLocality" content="Berlin"></span> <span itemprop="addressCountry" content="Germany"></span> </span> <span itemprop="startDate" content="2017-04-12T00:00:00+00:00"></span> <span itemprop="endDate" content="2017-04-13T00:00:00+00:00"></span> <span itemprop="offers" itemscope itemtype="http://schema.org/Offer"> <span itemprop="url" content="https://sensiolabs.de/de/trainings/sf3c2.html"></span> </span> </a> </li> <li itemscope itemtype="http://schema.org/Event"> <a itemprop="url" href="http://training.sensiolabs.com/en/courses?q=SF3C4&amp;from=05/02/2017&amp;to=05/05/2017"> <span itemprop="name">Web Development with Symfony 3</span> <br /> <span class="metadata">Paris - 2017-05-02</span> <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <span itemprop="addressLocality" content="Paris"></span> <span itemprop="addressCountry" content="France"></span> </span> <span itemprop="startDate" content="2017-05-02T00:00:00+00:00"></span> <span itemprop="endDate" content="2017-05-05T00:00:00+00:00"></span> <span itemprop="offers" itemscope itemtype="http://schema.org/Offer"> <span itemprop="url" content="http://training.sensiolabs.com/en/courses/SF3C4-web-development-with-symfony3"></span> </span> </a> </li> <li itemscope itemtype="http://schema.org/Event"> <a itemprop="url" href="http://training.sensiolabs.com/en/courses?q=SF3C1&amp;from=05/02/2017&amp;to=05/03/2017"> <span itemprop="name">Getting Started with Symfony 3</span> <br /> <span class="metadata">Paris - 2017-05-02</span> <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <span itemprop="addressLocality" content="Paris"></span> <span itemprop="addressCountry" content="France"></span> </span> <span itemprop="startDate" content="2017-05-02T00:00:00+00:00"></span> <span itemprop="endDate" content="2017-05-03T00:00:00+00:00"></span> <span itemprop="offers" itemscope itemtype="http://schema.org/Offer"> <span itemprop="url" content="http://training.sensiolabs.com/en/courses/SF3C1-getting-started-with-symfony3"></span> </span> </a> </li> </ul> <div class="link_01"> <a href="https://training.sensiolabs.com/" class="png_fix">View all sessions</a> </div> </div> </div> </div> </div> <div class="sitemap"> <div class="container"> <h5> <img src="http://symfony.com/images/v5/logos/sf-white.svg" alt="Symfony" /> is a trademark of Fabien Potencier. All rights reserved. </h5> <ul class="row sitemap-list list-unstyled"> <li class="col-xs-4 col-md-2"> <h6><a href="/what-is-symfony">What is Symfony?</a></h6> <ul class="list_menu_footer list-unstyled"> <li> <a href="/at-a-glance"> Symfony at a Glance </a> </li> <li> <a href="/components"> Symfony Components </a> </li> <li> <a href="/projects"> Projects using Symfony </a> </li> <li> <a href="/blog/category/case-studies"> Case Studies </a> </li> <li> <a href="/roadmap"> Symfony Roadmap </a> </li> <li> <a href="/doc/current/contributing/code/security.html"> Security Policy </a> </li> <li> <a href="/logo"> Logo &amp;
Screenshots </a> </li> <li> <a href="/license"> Trademark &amp;
Licenses </a> </li> <li> <a href="/legacy"> symfony1 Legacy </a> </li> </ul> </li> <li class="col-xs-4 col-md-2"> <h6><a href="/doc/current/index.html">Learn Symfony</a></h6> <ul class="list_menu_footer list-unstyled"> <li class=""> <a href="/doc/3.2/setup.html">Getting Started</a> </li> <li class=""> <a href="/doc/3.2/components/index.html">Components</a> </li> <li class=""> <a href="/doc/3.2/best_practices/index.html">Best Practices</a> </li> <li class=""> <a href="/doc/bundles/">Bundles</a> </li> <li class=""> <a href="/doc/3.2/reference/index.html">Reference</a> </li> <li class=""> <a href="https://training.sensiolabs.com/en/courses?q=symfony">Training</a> </li> <li class=""> <a href="https://sensiolabs.com/certification">Certification</a> </li> </ul> </li> <li class="col-xs-4 col-md-2"> <ul class="list-unstyled list_menu_footer"> <li> <h6><a href="/community">Community</a></h6> <ul class="list_menu_footer list-unstyled"> <li> <a href="https://connect.sensiolabs.com/"> SensioLabs Connect </a> </li> <li> <a href="/support"> Support </a> </li> <li> <a href="/doc/current/contributing/index.html"> How to be Involved </a> </li> <li> <a href="/stats/code"> Code Stats </a> </li> <li> <a href="/stats/downloads"> Downloads Stats </a> </li> <li> <a href="/contributors"> Contributors </a> </li> </ul> </li> </ul> </li> <li class="col-xs-4 col-md-2"> <ul class="list-unstyled list_menu_footer"> <li> <h6><a href="/blog/">Blog</a></h6> <ul class="list_menu_footer list-unstyled"> <li class=""> <a href="/blog/category/a-week-of-symfony">A week of symfony</a> </li> <li class=""> <a href="/blog/category/case-studies">Case studies</a> </li> <li class=""> <a href="/blog/category/community">Community</a> </li> <li class=""> <a href="/blog/category/documentation">Documentation</a> </li> <li class=""> <a href="/blog/category/living-on-the-edge">Living on the edge</a> </li> <li class=""> <a href="/blog/category/meet-the-bundle">Meet the Bundle</a> </li> <li class=""> <a href="/blog/category/releases">Releases</a> </li> <li class=""> <a href="/blog/category/security-advisories">Security Advisories</a> </li> <li class=""> <a href="/blog/category/symfony-cmf">Symfony CMF</a> </li> <li><a href="/events/"><em>Community Events</em></a></li> </ul> </li> </ul> </li> <li class="col-xs-4 col-md-2"> <h6><a href="/services">Services</a></h6> <ul class="list_menu_footer list-unstyled"> <li><a href="/services">Our services</a></li> <li><a href="https://training.sensiolabs.com/en">Train developers</a></li> <li><a href="/services#before">Start a project</a></li> <li><a href="https://insight.sensiolabs.com/">Manage your project quality</a></li> <li><a href="https://blackfire.io/">Improve your project performance</a></li> <li><a href="/services">Struggling with your project</a></li> <li><a href="https://sensiolabs.com/en/packaged-solutions/index.html">Support</a></li> <li><a href="/services">Contact us</a></li> </ul> </li> <li class="col-xs-4 col-md-2"> <h6><a href="/about">About</a></h6> <ul class="list_menu_footer list-unstyled"> <li> <a href="https://sensiolabs.com/en/join_us/join_us.html"> SensioLabs </a> </li> <li> <a href="/contributors"> Contributors </a> </li> <li> <a href="https://sensiolabs.com/en/join_us/join_us.html"> Jobs </a> </li> <li> <a href="/support"> Support </a> </li> </ul> </li> </ul> </div> </div> </footer> </div> <script type="text/javascript"> // load SensioLabsConnect bar window.addEventListener('load', function() { var js, fjs = document.getElementsByTagName('script')[0], load = function(url, id) { if (document.getElementById(id)) { return; } js = document.createElement('script'); js.src = url; js.id = id; fjs.parentNode.insertBefore(js, fjs); }; load('https://connect.sensiolabs.com/sln.js?customize_url=http://symfony.com/sln_customiser.js', 'sln_bar'); var s = document.createElement('link'); s.rel = 'stylesheet'; s.href = 'https://connect.sensiolabs.com/css/sln.css'; var x = document.getElementsByTagName('title')[0]; x.parentNode.insertBefore(s, x); } ); </script>
</body>
</html>
</body>
</html>

Symfony.com - Script Codes CSS Codes

#carbonads { display: block; overflow: hidden; margin-bottom: 2em; padding: 1.5em; max-width: calc(230px + 2em); background-color: #f5f5f5; text-align: center; font-size: 12px; line-height: 1.5;
}
#carbonads a { color: #63606b; transition: color .2s ease-in-out;
}
#carbonads a:hover { color: inherit; text-decoration: none;
}
#carbonads span { display: block; overflow: hidden;
}
.carbon-img { display: block; margin: 0 auto 1em;
}
.carbon-text { display: block; margin-bottom: .5em;
}
.carbon-poweredby { display: block; color: #97949e; text-transform: uppercase; font-size: 9px;
}
@media only screen and (min-width: 320px) and (max-width: 759px) { #carbonads { float: none; margin: 0 auto 1em; max-width: 330px; border-radius: 3px; background-color: #ffffff; } #carbonads span { position: relative; } #carbonads > span { max-width: none; } .carbon-img { float: left; margin: 0 1em 0 0; } .carbon-text { float: left; max-width: calc(100% - 130px - 1em); text-align: left; } .carbon-poweredby { position: absolute; right: 0; bottom: 0; display: block; }
}
Symfony.com - Script Codes
Symfony.com - Script Codes
Home Page Home
Developer Carbon Ads
Username carbonads
Uploaded December 12, 2022
Rating 3
Size 19,712 Kb
Views 6,072
Do you need developer help for Symfony.com?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Carbon Ads (carbonads) Script Codes
Create amazing SEO content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!