mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
948 lines
77 KiB
HTML
948 lines
77 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
||
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
||
<head>
|
||
<meta charset="utf-8">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<title>URI Routing — CodeIgniter 4.0.4 documentation</title>
|
||
|
||
|
||
|
||
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
|
||
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
|
||
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
|
||
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script type="text/javascript" src="../_static/jquery.js"></script>
|
||
<script type="text/javascript" src="../_static/underscore.js"></script>
|
||
<script type="text/javascript" src="../_static/doctools.js"></script>
|
||
<script type="text/javascript" src="../_static/language_data.js"></script>
|
||
<script type="text/javascript" src="../_static/js/citheme.js"></script>
|
||
<script type="text/javascript" src="../_static/js/carbon.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/js/theme.js"></script>
|
||
|
||
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="Controller Filters" href="filters.html" />
|
||
<link rel="prev" title="Controllers" href="controllers.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
|
||
|
||
<div class="wy-grid-for-nav">
|
||
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" style="background: #DD4814" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
|
||
|
||
|
||
<img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
|
||
|
||
</a>
|
||
|
||
|
||
|
||
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Controllers and Routing</a><ul class="current">
|
||
<li class="toctree-l2"><a class="reference internal" href="controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Dates and Times</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</a>
|
||
|
||
</nav>
|
||
|
||
|
||
<div class="wy-nav-content">
|
||
|
||
<div class="rst-content">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="navigation" aria-label="breadcrumbs navigation">
|
||
|
||
<ul class="wy-breadcrumbs">
|
||
|
||
<li><a href="../index.html" class="icon icon-home"></a> »</li>
|
||
|
||
<li><a href="index.html">Controllers and Routing</a> »</li>
|
||
|
||
<li>URI Routing</li>
|
||
|
||
|
||
<li class="wy-breadcrumbs-aside">
|
||
|
||
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<div class="section" id="uri-routing">
|
||
<h1>URI Routing<a class="headerlink" href="#uri-routing" title="Permalink to this headline">¶</a></h1>
|
||
<div class="contents local topic" id="contents">
|
||
<ul class="simple">
|
||
<li><a class="reference internal" href="#setting-your-own-routing-rules" id="id1">Setting your own routing rules</a></li>
|
||
<li><a class="reference internal" href="#placeholders" id="id2">Placeholders</a></li>
|
||
<li><a class="reference internal" href="#examples" id="id3">Examples</a></li>
|
||
<li><a class="reference internal" href="#custom-placeholders" id="id4">Custom Placeholders</a></li>
|
||
<li><a class="reference internal" href="#regular-expressions" id="id5">Regular Expressions</a></li>
|
||
<li><a class="reference internal" href="#closures" id="id6">Closures</a></li>
|
||
<li><a class="reference internal" href="#mapping-multiple-routes" id="id7">Mapping multiple routes</a></li>
|
||
<li><a class="reference internal" href="#redirecting-routes" id="id8">Redirecting Routes</a></li>
|
||
<li><a class="reference internal" href="#grouping-routes" id="id9">Grouping Routes</a></li>
|
||
<li><a class="reference internal" href="#environment-restrictions" id="id10">Environment Restrictions</a></li>
|
||
<li><a class="reference internal" href="#reverse-routing" id="id11">Reverse Routing</a></li>
|
||
<li><a class="reference internal" href="#using-named-routes" id="id12">Using Named Routes</a></li>
|
||
<li><a class="reference internal" href="#using-http-verbs-in-routes" id="id13">Using HTTP verbs in routes</a></li>
|
||
<li><a class="reference internal" href="#command-line-only-routes" id="id14">Command-Line only Routes</a></li>
|
||
<li><a class="reference internal" href="#global-options" id="id15">Global Options</a><ul>
|
||
<li><a class="reference internal" href="#applying-filters" id="id16">Applying Filters</a></li>
|
||
<li><a class="reference internal" href="#assigning-namespace" id="id17">Assigning Namespace</a></li>
|
||
<li><a class="reference internal" href="#limit-to-hostname" id="id18">Limit to Hostname</a></li>
|
||
<li><a class="reference internal" href="#limit-to-subdomains" id="id19">Limit to Subdomains</a></li>
|
||
<li><a class="reference internal" href="#offsetting-the-matched-parameters" id="id20">Offsetting the Matched Parameters</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#routes-configuration-options" id="id21">Routes Configuration Options</a><ul>
|
||
<li><a class="reference internal" href="#default-namespace" id="id22">Default Namespace</a></li>
|
||
<li><a class="reference internal" href="#default-controller" id="id23">Default Controller</a></li>
|
||
<li><a class="reference internal" href="#default-method" id="id24">Default Method</a></li>
|
||
<li><a class="reference internal" href="#translate-uri-dashes" id="id25">Translate URI Dashes</a></li>
|
||
<li><a class="reference internal" href="#use-defined-routes-only" id="id26">Use Defined Routes Only</a></li>
|
||
<li><a class="reference internal" href="#override" id="id27">404 Override</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<p>Typically there is a one-to-one relationship between a URL string and its corresponding
|
||
controller class/method. The segments in a URI normally follow this pattern:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">class</span><span class="o">/</span><span class="nx">method</span><span class="o">/</span><span class="nx">id</span><span class="o">/</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In some instances, however, you may want to remap this relationship so that a different
|
||
class/method can be called instead of the one corresponding to the URL.</p>
|
||
<p>For example, let’s say you want your URLs to have this prototype:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">product</span><span class="o">/</span><span class="mi">1</span><span class="o">/</span>
|
||
<span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">product</span><span class="o">/</span><span class="mi">2</span><span class="o">/</span>
|
||
<span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">product</span><span class="o">/</span><span class="mi">3</span><span class="o">/</span>
|
||
<span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">product</span><span class="o">/</span><span class="mi">4</span><span class="o">/</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Normally the second segment of the URL is reserved for the method name, but in the example
|
||
above it instead has a product ID. To overcome this, CodeIgniter allows you to remap the URI handler.</p>
|
||
<div class="section" id="setting-your-own-routing-rules">
|
||
<h2><a class="toc-backref" href="#id1">Setting your own routing rules</a><a class="headerlink" href="#setting-your-own-routing-rules" title="Permalink to this headline">¶</a></h2>
|
||
<p>Routing rules are defined in the <strong>app/config/Routes.php</strong> file. In it you’ll see that
|
||
it creates an instance of the RouteCollection class that permits you to specify your own routing criteria.
|
||
Routes can be specified using placeholders or Regular Expressions.</p>
|
||
<p>A route simply takes the URI on the left, and maps it to the controller and method on the right,
|
||
along with any parameters that should be passed to the controller. The controller and method should
|
||
be listed in the same way that you would use a static method, by separating the fully-namespaced class
|
||
and its method with a double-colon, like <code class="docutils literal notranslate"><span class="pre">Users::list</span></code>. If that method requires parameters to be
|
||
passed to it, then they would be listed after the method name, separated by forward-slashes:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Calls the $Users->list()</span>
|
||
<span class="nx">Users</span><span class="o">::</span><span class="na">list</span>
|
||
<span class="c1">// Calls $Users->list(1, 23)</span>
|
||
<span class="nx">Users</span><span class="o">::</span><span class="na">list</span><span class="o">/</span><span class="mi">1</span><span class="o">/</span><span class="mi">23</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="placeholders">
|
||
<h2><a class="toc-backref" href="#id2">Placeholders</a><a class="headerlink" href="#placeholders" title="Permalink to this headline">¶</a></h2>
|
||
<p>A typical route might look something like this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'product/(:num)'</span><span class="p">,</span> <span class="s1">'App\Catalog::productLookup'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In a route, the first parameter contains the URI to be matched, while the second parameter
|
||
contains the destination it should be re-routed to. In the above example, if the literal word
|
||
“product” is found in the first segment of the URL, and a number is found in the second segment,
|
||
the “AppCatalog” class and the “productLookup” method are used instead.</p>
|
||
<p>Placeholders are simply strings that represent a Regular Expression pattern. During the routing
|
||
process, these placeholders are replaced with the value of the Regular Expression. They are primarily
|
||
used for readability.</p>
|
||
<p>The following placeholders are available for you to use in your routes:</p>
|
||
<table border="1" class="docutils">
|
||
<colgroup>
|
||
<col width="8%" />
|
||
<col width="92%" />
|
||
</colgroup>
|
||
<thead valign="bottom">
|
||
<tr class="row-odd"><th class="head">Placeholders</th>
|
||
<th class="head">Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody valign="top">
|
||
<tr class="row-even"><td>(:any)</td>
|
||
<td>will match all characters from that point to the end of the URI. This may include multiple URI segments.</td>
|
||
</tr>
|
||
<tr class="row-odd"><td>(:segment)</td>
|
||
<td>will match any character except for a forward slash (/) restricting the result to a single segment.</td>
|
||
</tr>
|
||
<tr class="row-even"><td>(:num)</td>
|
||
<td>will match any integer.</td>
|
||
</tr>
|
||
<tr class="row-odd"><td>(:alpha)</td>
|
||
<td>will match any string of alphabetic characters</td>
|
||
</tr>
|
||
<tr class="row-even"><td>(:alphanum)</td>
|
||
<td>will match any string of alphabetic characters or integers, or any combination of the two.</td>
|
||
</tr>
|
||
<tr class="row-odd"><td>(:hash)</td>
|
||
<td>is the same as <strong>(:segment)</strong>, but can be used to easily see which routes use hashed ids (see the <a class="reference internal" href="../models/model.html"><span class="doc">Model</span></a> docs).</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition note">
|
||
<p class="first admonition-title">Note</p>
|
||
<p class="last"><strong>{locale}</strong> cannot be used as a placeholder or other part of the route, as it is reserved for use
|
||
in <a class="reference internal" href="../outgoing/localization.html"><span class="doc">localization</span></a>.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="examples">
|
||
<h2><a class="toc-backref" href="#id3">Examples</a><a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
|
||
<p>Here are a few basic routing examples.</p>
|
||
<p>A URL containing the word “journals” in the first segment will be remapped to the “AppBlogs” class,
|
||
and the default method, which is usually <code class="docutils literal notranslate"><span class="pre">index()</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'journals'</span><span class="p">,</span> <span class="s1">'App\Blogs'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>A URL containing the segments “blog/joe” will be remapped to the “Blogs” class and the “users” method.
|
||
The ID will be set to “34”:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'blog/joe'</span><span class="p">,</span> <span class="s1">'Blogs::users/34'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>A URL with “product” as the first segment, and anything in the second will be remapped to the “Catalog” class
|
||
and the “productLookup” method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'product/(:any)'</span><span class="p">,</span> <span class="s1">'Catalog::productLookup'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>A URL with “product” as the first segment, and a number in the second will be remapped to the “Catalog” class
|
||
and the “productLookupByID” method passing in the match as a variable to the method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'product/(:num)'</span><span class="p">,</span> <span class="s1">'Catalog::productLookupByID/$1'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Note that a single <code class="docutils literal notranslate"><span class="pre">(:any)</span></code> will match multiple segments in the URL if present. For example the route:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'product/(:any)'</span><span class="p">,</span> <span class="s1">'Catalog::productLookup/$1'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>will match product/123, product/123/456, product/123/456/789 and so on. The implementation in the
|
||
Controller should take into account the maximum parameters:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">function</span> <span class="nf">productLookup</span><span class="p">(</span><span class="nv">$seg1</span> <span class="o">=</span> <span class="k">false</span><span class="p">,</span> <span class="nv">$seg2</span> <span class="o">=</span> <span class="k">false</span><span class="p">,</span> <span class="nv">$seg3</span> <span class="o">=</span> <span class="k">false</span><span class="p">)</span> <span class="p">{</span>
|
||
<span class="k">echo</span> <span class="nv">$seg1</span><span class="p">;</span> <span class="c1">// Will be 123 in all examples</span>
|
||
<span class="k">echo</span> <span class="nv">$seg2</span><span class="p">;</span> <span class="c1">// false in first, 456 in second and third example</span>
|
||
<span class="k">echo</span> <span class="nv">$seg3</span><span class="p">;</span> <span class="c1">// false in first and second, 789 in third</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If matching multiple segments is not the intended behavior, <code class="docutils literal notranslate"><span class="pre">(:segment)</span></code> should be used when defining the
|
||
routes. With the examples URLs from above:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'product/(:segment)'</span><span class="p">,</span> <span class="s1">'Catalog::productLookup/$1'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>will only match product/123 and generate 404 errors for other example.</p>
|
||
<div class="admonition important">
|
||
<p class="first admonition-title">Important</p>
|
||
<p class="last">While the <code class="docutils literal notranslate"><span class="pre">add()</span></code> method is convenient, it is recommended to always use the HTTP-verb-based
|
||
routes, described below, as it is more secure. It will also provide a slight performance increase, since
|
||
only routes that match the current request method are stored, resulting in fewer routes to scan through
|
||
when trying to find a match.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="custom-placeholders">
|
||
<h2><a class="toc-backref" href="#id4">Custom Placeholders</a><a class="headerlink" href="#custom-placeholders" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can create your own placeholders that can be used in your routes file to fully customize the experience
|
||
and readability.</p>
|
||
<p>You add new placeholders with the <code class="docutils literal notranslate"><span class="pre">addPlaceholder</span></code> method. The first parameter is the string to be used as
|
||
the placeholder. The second parameter is the Regular Expression pattern it should be replaced with.
|
||
This must be called before you add the route:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">addPlaceholder</span><span class="p">(</span><span class="s1">'uuid'</span><span class="p">,</span> <span class="s1">'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users/(:uuid)'</span><span class="p">,</span> <span class="s1">'Users::show/$1'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="regular-expressions">
|
||
<h2><a class="toc-backref" href="#id5">Regular Expressions</a><a class="headerlink" href="#regular-expressions" title="Permalink to this headline">¶</a></h2>
|
||
<p>If you prefer you can use regular expressions to define your routing rules. Any valid regular expression
|
||
is allowed, as are back-references.</p>
|
||
<div class="admonition important">
|
||
<p class="first admonition-title">Important</p>
|
||
<p>Note: If you use back-references you must use the dollar syntax rather than the double backslash syntax.
|
||
A typical RegEx route might look something like this:</p>
|
||
<div class="last highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'products/([a-z]+)/(\d+)'</span><span class="p">,</span> <span class="s1">'Products::show/$1/id_$2'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<p>In the above example, a URI similar to products/shirts/123 would instead call the <code class="docutils literal notranslate"><span class="pre">show</span></code> method
|
||
of the <code class="docutils literal notranslate"><span class="pre">Products</span></code> controller class, with the original first and second segment passed as arguments to it.</p>
|
||
<p>With regular expressions, you can also catch a segment containing a forward slash (‘/’), which would usually
|
||
represent the delimiter between multiple segments.</p>
|
||
<p>For example, if a user accesses a password protected area of your web application and you wish to be able to
|
||
redirect them back to the same page after they log in, you may find this example useful:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'login/(.+)'</span><span class="p">,</span> <span class="s1">'Auth::login/$1'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For those of you who don’t know regular expressions and want to learn more about them,
|
||
<a class="reference external" href="https://www.regular-expressions.info/">regular-expressions.info</a> might be a good starting point.</p>
|
||
<div class="admonition important">
|
||
<p class="first admonition-title">Important</p>
|
||
<p class="last">Note: You can also mix and match wildcards with regular expressions.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="closures">
|
||
<h2><a class="toc-backref" href="#id6">Closures</a><a class="headerlink" href="#closures" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can use an anonymous function, or Closure, as the destination that a route maps to. This function will be
|
||
executed when the user visits that URI. This is handy for quickly executing small tasks, or even just showing
|
||
a simple view:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'feed'</span><span class="p">,</span> <span class="k">function</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$rss</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">RSSFeeder</span><span class="p">();</span>
|
||
<span class="k">return</span> <span class="nv">$rss</span><span class="o">-></span><span class="na">feed</span><span class="p">(</span><span class="s1">'general'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="mapping-multiple-routes">
|
||
<h2><a class="toc-backref" href="#id7">Mapping multiple routes</a><a class="headerlink" href="#mapping-multiple-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>While the add() method is simple to use, it is often handier to work with multiple routes at once, using
|
||
the <code class="docutils literal notranslate"><span class="pre">map()</span></code> method. Instead of calling the <code class="docutils literal notranslate"><span class="pre">add()</span></code> method for each route that you need to add, you can
|
||
define an array of routes and then pass it as the first parameter to the <code class="docutils literal notranslate"><span class="pre">map()</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span> <span class="o">=</span> <span class="p">[];</span>
|
||
<span class="nv">$routes</span><span class="p">[</span><span class="s1">'product/(:num)'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Catalog::productLookupById'</span><span class="p">;</span>
|
||
<span class="nv">$routes</span><span class="p">[</span><span class="s1">'product/(:alphanum)'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Catalog::productLookupByName'</span><span class="p">;</span>
|
||
|
||
<span class="nv">$collection</span><span class="o">-></span><span class="na">map</span><span class="p">(</span><span class="nv">$routes</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="redirecting-routes">
|
||
<h2><a class="toc-backref" href="#id8">Redirecting Routes</a><a class="headerlink" href="#redirecting-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>Any site that lives long enough is bound to have pages that move. You can specify routes that should redirect
|
||
to other routes with the <code class="docutils literal notranslate"><span class="pre">addRedirect()</span></code> method. The first parameter is the URI pattern for the old route. The
|
||
second parameter is either the new URI to redirect to, or the name of a named route. The third parameter is
|
||
the HTTP status code that should be sent along with the redirect. The default value is <code class="docutils literal notranslate"><span class="pre">302</span></code> which is a temporary
|
||
redirect and is recommended in most cases:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users/profile'</span><span class="p">,</span> <span class="s1">'Users::profile'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'as'</span> <span class="o">=></span> <span class="s1">'profile'</span><span class="p">]);</span>
|
||
|
||
<span class="c1">// Redirect to a named route</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">addRedirect</span><span class="p">(</span><span class="s1">'users/about'</span><span class="p">,</span> <span class="s1">'profile'</span><span class="p">);</span>
|
||
<span class="c1">// Redirect to a URI</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">addRedirect</span><span class="p">(</span><span class="s1">'users/about'</span><span class="p">,</span> <span class="s1">'users/profile'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If a redirect route is matched during a page load, the user will be immediately redirected to the new page before a
|
||
controller can be loaded.</p>
|
||
</div>
|
||
<div class="section" id="grouping-routes">
|
||
<h2><a class="toc-backref" href="#id9">Grouping Routes</a><a class="headerlink" href="#grouping-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can group your routes under a common name with the <code class="docutils literal notranslate"><span class="pre">group()</span></code> method. The group name becomes a segment that
|
||
appears prior to the routes defined inside of the group. This allows you to reduce the typing needed to build out an
|
||
extensive set of routes that all share the opening string, like when building an admin area:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'admin'</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="s1">'Admin\Users::index'</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'blog'</span><span class="p">,</span> <span class="s1">'Admin\Blog::index'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This would prefix the ‘users’ and ‘blog” URIs with “admin”, handling URLs like <code class="docutils literal notranslate"><span class="pre">/admin/users</span></code> and <code class="docutils literal notranslate"><span class="pre">/admin/blog</span></code>.
|
||
It is possible to nest groups within groups for finer organization if you need it:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'admin'</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'list'</span><span class="p">,</span> <span class="s1">'Admin\Users::list'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This would handle the URL at <code class="docutils literal notranslate"><span class="pre">admin/users/list</span></code>.</p>
|
||
<p>If you need to assign options to a group, like a <a class="reference external" href="#assigning-namespace">namespace</a>, do it before the callback:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'api'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'namespace'</span> <span class="o">=></span> <span class="s1">'App\API\v1'</span><span class="p">],</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">resource</span><span class="p">(</span><span class="s1">'users'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This would handle a resource route to the <code class="docutils literal notranslate"><span class="pre">App\API\v1\Users</span></code> controller with the <code class="docutils literal notranslate"><span class="pre">/api/users</span></code> URI.</p>
|
||
<p>You can also use a specific <a class="reference external" href="filters.html">filter</a> for a group of routes. This will always
|
||
run the filter before or after the controller. This is especially handy during authentication or api logging:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'api'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'filter'</span> <span class="o">=></span> <span class="s1">'api-auth'</span><span class="p">],</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">resource</span><span class="p">(</span><span class="s1">'users'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The value for the filter must match one of the aliases defined within <code class="docutils literal notranslate"><span class="pre">app/Config/Filters.php</span></code>.</p>
|
||
</div>
|
||
<div class="section" id="environment-restrictions">
|
||
<h2><a class="toc-backref" href="#id10">Environment Restrictions</a><a class="headerlink" href="#environment-restrictions" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can create a set of routes that will only be viewable in a certain environment. This allows you to create
|
||
tools that only the developer can use on their local machines that are not reachable on testing or production servers.
|
||
This can be done with the <code class="docutils literal notranslate"><span class="pre">environment()</span></code> method. The first parameter is the name of the environment. Any
|
||
routes defined within this closure are only accessible from the given environment:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">environment</span><span class="p">(</span><span class="s1">'development'</span><span class="p">,</span> <span class="k">function</span><span class="p">(</span><span class="nv">$routes</span><span class="p">)</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'builder'</span><span class="p">,</span> <span class="s1">'Tools\Builder::index'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="reverse-routing">
|
||
<h2><a class="toc-backref" href="#id11">Reverse Routing</a><a class="headerlink" href="#reverse-routing" title="Permalink to this headline">¶</a></h2>
|
||
<p>Reverse routing allows you to define the controller and method, as well as any parameters, that a link should go
|
||
to, and have the router lookup the current route to it. This allows route definitions to change without you having
|
||
to update your application code. This is typically used within views to create links.</p>
|
||
<p>For example, if you have a route to a photo gallery that you want to link to, you can use the <code class="docutils literal notranslate"><span class="pre">route_to()</span></code> helper
|
||
function to get the current route that should be used. The first parameter is the fully qualified Controller and method,
|
||
separated by a double colon (::), much like you would use when writing the initial route itself. Any parameters that
|
||
should be passed to the route are passed in next:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// The route is defined as:</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users/(:num)/gallery(:any)'</span><span class="p">,</span> <span class="s1">'App\Controllers\Galleries::showUserGallery/$1/$2'</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Generate the relative URL to link to user ID 15, gallery 12</span>
|
||
<span class="c1">// Generates: /users/15/gallery/12</span>
|
||
<span class="o"><</span><span class="nx">a</span> <span class="nx">href</span><span class="o">=</span><span class="s2">"<?= route_to('App\Controllers\Galleries::showUserGallery', 15, 12) ?>"</span><span class="o">></span><span class="nx">View</span> <span class="nx">Gallery</span><span class="o"></</span><span class="nx">a</span><span class="o">></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="using-named-routes">
|
||
<h2><a class="toc-backref" href="#id12">Using Named Routes</a><a class="headerlink" href="#using-named-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can name routes to make your application less fragile. This applies a name to a route that can be called
|
||
later, and even if the route definition changes, all of the links in your application built with <code class="docutils literal notranslate"><span class="pre">route_to</span></code>
|
||
will still work without you having to make any changes. A route is named by passing in the <code class="docutils literal notranslate"><span class="pre">as</span></code> option
|
||
with the name of the route:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// The route is defined as:</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users/(:num)/gallery(:any)'</span><span class="p">,</span> <span class="s1">'Galleries::showUserGallery/$1/$2'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'as'</span> <span class="o">=></span> <span class="s1">'user_gallery'</span><span class="p">]);</span>
|
||
|
||
<span class="c1">// Generate the relative URL to link to user ID 15, gallery 12</span>
|
||
<span class="c1">// Generates: /users/15/gallery/12</span>
|
||
<span class="o"><</span><span class="nx">a</span> <span class="nx">href</span><span class="o">=</span><span class="s2">"<?= route_to('user_gallery', 15, 12) ?>"</span><span class="o">></span><span class="nx">View</span> <span class="nx">Gallery</span><span class="o"></</span><span class="nx">a</span><span class="o">></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This has the added benefit of making the views more readable, too.</p>
|
||
</div>
|
||
<div class="section" id="using-http-verbs-in-routes">
|
||
<h2><a class="toc-backref" href="#id13">Using HTTP verbs in routes</a><a class="headerlink" href="#using-http-verbs-in-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>It is possible to use HTTP verbs (request method) to define your routing rules. This is particularly
|
||
useful when building RESTFUL applications. You can use any standard HTTP verb (GET, POST, PUT, DELETE, etc).
|
||
Each verb has its own method you can use:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'products'</span><span class="p">,</span> <span class="s1">'Product::feature'</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">post</span><span class="p">(</span><span class="s1">'products'</span><span class="p">,</span> <span class="s1">'Product::feature'</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">put</span><span class="p">(</span><span class="s1">'products/(:num)'</span><span class="p">,</span> <span class="s1">'Product::feature'</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">delete</span><span class="p">(</span><span class="s1">'products/(:num)'</span><span class="p">,</span> <span class="s1">'Product::feature'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can supply multiple verbs that a route should match by passing them in as an array to the <code class="docutils literal notranslate"><span class="pre">match</span></code> method:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">match</span><span class="p">([</span><span class="s1">'get'</span><span class="p">,</span> <span class="s1">'put'</span><span class="p">],</span> <span class="s1">'products'</span><span class="p">,</span> <span class="s1">'Product::feature'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="command-line-only-routes">
|
||
<h2><a class="toc-backref" href="#id14">Command-Line only Routes</a><a class="headerlink" href="#command-line-only-routes" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can create routes that work only from the command-line, and are inaccessible from the web browser, with the
|
||
<code class="docutils literal notranslate"><span class="pre">cli()</span></code> method. This is great for building cronjobs or CLI-only tools. Any route created by any of the HTTP-verb-based
|
||
route methods will also be inaccessible from the CLI, but routes created by the <code class="docutils literal notranslate"><span class="pre">any()</span></code> method will still be
|
||
available from the command line:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">cli</span><span class="p">(</span><span class="s1">'migrate'</span><span class="p">,</span> <span class="s1">'App\Database::migrate'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="global-options">
|
||
<h2><a class="toc-backref" href="#id15">Global Options</a><a class="headerlink" href="#global-options" title="Permalink to this headline">¶</a></h2>
|
||
<p>All of the methods for creating a route (add, get, post, <a class="reference external" href="restful.html">resource</a> etc) can take an array of options that
|
||
can modify the generated routes, or further restrict them. The <code class="docutils literal notranslate"><span class="pre">$options</span></code> array is always the last parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">post</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">put</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">head</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">options</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">delete</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">patch</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">match</span><span class="p">([</span><span class="s1">'get'</span><span class="p">,</span> <span class="s1">'put'</span><span class="p">],</span> <span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">resource</span><span class="p">(</span><span class="s1">'photos'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">map</span><span class="p">(</span><span class="nv">$array</span><span class="p">,</span> <span class="nv">$options</span><span class="p">);</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">group</span><span class="p">(</span><span class="s1">'name'</span><span class="p">,</span> <span class="nv">$options</span><span class="p">,</span> <span class="k">function</span><span class="p">());</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="section" id="applying-filters">
|
||
<h3><a class="toc-backref" href="#id16">Applying Filters</a><a class="headerlink" href="#applying-filters" title="Permalink to this headline">¶</a></h3>
|
||
<p>You can alter the behavior of specific routes by supplying a filter to run before or after the controller. This is especially handy during authentication or api logging:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'admin'</span><span class="p">,</span><span class="s1">' AdminController::index'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'filter'</span> <span class="o">=></span> <span class="s1">'admin-auth'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The value for the filter must match one of the aliases defined within <code class="docutils literal notranslate"><span class="pre">app/Config/Filters.php</span></code>. You may also supply arguments to be passed to the filter’s <code class="docutils literal notranslate"><span class="pre">before()</span></code> and <code class="docutils literal notranslate"><span class="pre">after()</span></code> methods:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users/delete/(:segment)'</span><span class="p">,</span> <span class="s1">'AdminController::index'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'filter'</span> <span class="o">=></span> <span class="s1">'admin-auth:dual,noreturn'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>See <a class="reference external" href="filters.html">Controller filters</a> for more information on setting up filters.</p>
|
||
</div>
|
||
<div class="section" id="assigning-namespace">
|
||
<h3><a class="toc-backref" href="#id17">Assigning Namespace</a><a class="headerlink" href="#assigning-namespace" title="Permalink to this headline">¶</a></h3>
|
||
<p>While a default namespace will be prepended to the generated controllers (see below), you can also specify
|
||
a different namespace to be used in any options array, with the <code class="docutils literal notranslate"><span class="pre">namespace</span></code> option. The value should be the
|
||
namespace you want modified:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Routes to \Admin\Users::index()</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'admin/users'</span><span class="p">,</span> <span class="s1">'Users::index'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'namespace'</span> <span class="o">=></span> <span class="s1">'Admin'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The new namespace is only applied during that call for any methods that create a single route, like get, post, etc.
|
||
For any methods that create multiple routes, the new namespace is attached to all routes generated by that function
|
||
or, in the case of <code class="docutils literal notranslate"><span class="pre">group()</span></code>, all routes generated while in the closure.</p>
|
||
</div>
|
||
<div class="section" id="limit-to-hostname">
|
||
<h3><a class="toc-backref" href="#id18">Limit to Hostname</a><a class="headerlink" href="#limit-to-hostname" title="Permalink to this headline">¶</a></h3>
|
||
<p>You can restrict groups of routes to function only in certain domain or sub-domains of your application
|
||
by passing the “hostname” option along with the desired domain to allow it on as part of the options array:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$collection</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'hostname'</span> <span class="o">=></span> <span class="s1">'accounts.example.com'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This example would only allow the specified hosts to work if the domain exactly matched “accounts.example.com”.
|
||
It would not work under the main site at “example.com”.</p>
|
||
</div>
|
||
<div class="section" id="limit-to-subdomains">
|
||
<h3><a class="toc-backref" href="#id19">Limit to Subdomains</a><a class="headerlink" href="#limit-to-subdomains" title="Permalink to this headline">¶</a></h3>
|
||
<p>When the <code class="docutils literal notranslate"><span class="pre">subdomain</span></code> option is present, the system will restrict the routes to only be available on that
|
||
sub-domain. The route will only be matched if the subdomain is the one the application is being viewed through:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Limit to media.example.com</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'subdomain'</span> <span class="o">=></span> <span class="s1">'media'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>You can restrict it to any subdomain by setting the value to an asterisk, (*). If you are viewing from a URL
|
||
that does not have any subdomain present, this will not be matched:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Limit to any sub-domain</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'subdomain'</span> <span class="o">=></span> <span class="s1">'*'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="first admonition-title">Important</p>
|
||
<p class="last">The system is not perfect and should be tested for your specific domain before being used in production.
|
||
Most domains should work fine but some edge case ones, especially with a period in the domain itself (not used
|
||
to separate suffixes or www) can potentially lead to false positives.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="offsetting-the-matched-parameters">
|
||
<h3><a class="toc-backref" href="#id20">Offsetting the Matched Parameters</a><a class="headerlink" href="#offsetting-the-matched-parameters" title="Permalink to this headline">¶</a></h3>
|
||
<p>You can offset the matched parameters in your route by any numeric value with the <code class="docutils literal notranslate"><span class="pre">offset</span></code> option, with the
|
||
value being the number of segments to offset.</p>
|
||
<p>This can be beneficial when developing API’s with the first URI segment being the version number. It can also
|
||
be used when the first parameter is a language string:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">get</span><span class="p">(</span><span class="s1">'users/(:num)'</span><span class="p">,</span> <span class="s1">'users/show/$1'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'offset'</span> <span class="o">=></span> <span class="mi">1</span><span class="p">]);</span>
|
||
|
||
<span class="c1">// Creates:</span>
|
||
<span class="nv">$routes</span><span class="p">[</span><span class="s1">'users/(:num)'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'users/show/$2'</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="routes-configuration-options">
|
||
<h2><a class="toc-backref" href="#id21">Routes Configuration Options</a><a class="headerlink" href="#routes-configuration-options" title="Permalink to this headline">¶</a></h2>
|
||
<p>The RoutesCollection class provides several options that affect all routes, and can be modified to meet your
|
||
application’s needs. These options are available at the top of <cite>/app/Config/Routes.php</cite>.</p>
|
||
<div class="section" id="default-namespace">
|
||
<h3><a class="toc-backref" href="#id22">Default Namespace</a><a class="headerlink" href="#default-namespace" title="Permalink to this headline">¶</a></h3>
|
||
<p>When matching a controller to a route, the router will add the default namespace value to the front of the controller
|
||
specified by the route. By default, this value is empty, which leaves each route to specify the fully namespaced
|
||
controller:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">setDefaultNamespace</span><span class="p">(</span><span class="s1">''</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Controller is \Users</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="s1">'Users::index'</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Controller is \Admin\Users</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="s1">'Admin\Users::index'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If your controllers are not explicitly namespaced, there is no need to change this. If you namespace your controllers,
|
||
then you can change this value to save typing:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">setDefaultNamespace</span><span class="p">(</span><span class="s1">'App'</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Controller is \App\Users</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="s1">'Users::index'</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Controller is \App\Admin\Users</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">add</span><span class="p">(</span><span class="s1">'users'</span><span class="p">,</span> <span class="s1">'Admin\Users::index'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="default-controller">
|
||
<h3><a class="toc-backref" href="#id23">Default Controller</a><a class="headerlink" href="#default-controller" title="Permalink to this headline">¶</a></h3>
|
||
<p>When a user visits the root of your site (i.e. example.com) the controller to use is determined by the value set by
|
||
the <code class="docutils literal notranslate"><span class="pre">setDefaultController()</span></code> method, unless a route exists for it explicitly. The default value for this is <code class="docutils literal notranslate"><span class="pre">Home</span></code>
|
||
which matches the controller at <code class="docutils literal notranslate"><span class="pre">/app/Controllers/Home.php</span></code>:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// example.com routes to app/Controllers/Welcome.php</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">setDefaultController</span><span class="p">(</span><span class="s1">'Welcome'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The default controller is also used when no matching route has been found, and the URI would point to a directory
|
||
in the controllers directory. For example, if the user visits <code class="docutils literal notranslate"><span class="pre">example.com/admin</span></code>, if a controller was found at
|
||
<code class="docutils literal notranslate"><span class="pre">/app/Controllers/admin/Home.php</span></code> it would be used.</p>
|
||
</div>
|
||
<div class="section" id="default-method">
|
||
<h3><a class="toc-backref" href="#id24">Default Method</a><a class="headerlink" href="#default-method" title="Permalink to this headline">¶</a></h3>
|
||
<p>This works similar to the default controller setting, but is used to determine the default method that is used
|
||
when a controller is found that matches the URI, but no segment exists for the method. The default value is
|
||
<code class="docutils literal notranslate"><span class="pre">index</span></code>.</p>
|
||
<p>In this example, if the user were to visit example.com/products, and a Products controller existed, the
|
||
<code class="docutils literal notranslate"><span class="pre">Products::listAll()</span></code> method would be executed:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">setDefaultMethod</span><span class="p">(</span><span class="s1">'listAll'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="translate-uri-dashes">
|
||
<h3><a class="toc-backref" href="#id25">Translate URI Dashes</a><a class="headerlink" href="#translate-uri-dashes" title="Permalink to this headline">¶</a></h3>
|
||
<p>This option enables you to automatically replace dashes (‘-‘) with underscores in the controller and method
|
||
URI segments, thus saving you additional route entries if you need to do that. This is required because the
|
||
dash isn’t a valid class or method name character and would cause a fatal error if you try to use it:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">setTranslateURIDashes</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="use-defined-routes-only">
|
||
<h3><a class="toc-backref" href="#id26">Use Defined Routes Only</a><a class="headerlink" href="#use-defined-routes-only" title="Permalink to this headline">¶</a></h3>
|
||
<p>When no defined route is found that matches the URI, the system will attempt to match that URI against the
|
||
controllers and methods as described above. You can disable this automatic matching, and restrict routes
|
||
to only those defined by you, by setting the <code class="docutils literal notranslate"><span class="pre">setAutoRoute()</span></code> option to false:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$routes</span><span class="o">-></span><span class="na">setAutoRoute</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="override">
|
||
<h3><a class="toc-backref" href="#id27">404 Override</a><a class="headerlink" href="#override" title="Permalink to this headline">¶</a></h3>
|
||
<p>When a page is not found that matches the current URI, the system will show a generic 404 view. You can change
|
||
what happens by specifying an action to happen with the <code class="docutils literal notranslate"><span class="pre">set404Override()</span></code> option. The value can be either
|
||
a valid class/method pair, just like you would show in any route, or a Closure:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Would execute the show404 method of the App\Errors class</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">set404Override</span><span class="p">(</span><span class="s1">'App\Errors::show404'</span><span class="p">);</span>
|
||
|
||
<span class="c1">// Will display a custom view</span>
|
||
<span class="nv">$routes</span><span class="o">-></span><span class="na">set404Override</span><span class="p">(</span><span class="k">function</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'my_errors/not_found.html'</span><span class="p">);</span>
|
||
<span class="p">});</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<footer>
|
||
|
||
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
||
|
||
<a href="filters.html" class="btn btn-neutral float-right" title="Controller Filters" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
|
||
|
||
|
||
<a href="controllers.html" class="btn btn-neutral float-left" title="Controllers" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
|
||
|
||
</div>
|
||
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
|
||
© Copyright 2019-2020 CodeIgniter Foundation
|
||
<span class="lastupdated">
|
||
Last updated on Jul 20, 2020.
|
||
</span>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
|
||
|
||
<a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
|
||
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
</footer>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</section>
|
||
|
||
</div>
|
||
|
||
|
||
<script type="text/javascript">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html> |