- CodeIgniter Web Application Blueprints
- Rob Foster
- 290字
- 2021-08-06 19:34:29
Adjusting the routes.php file
We want short URLs—in fact the shorter the better. The user clicking on a URL would be better served if the URL were as short as possible; for that reason, it would be a good idea if we removed certain things from the URL to make it shorter—for example, the controller name and function name. We will use CodeIgniter's routing functionality to achieve this. This can be done as follows:
- Open the
config/routes.php
file for editing and find the following lines (near the bottom of the file):$route['default_controller'] = "welcome"; $route['404_override'] = '';
- Firstly, we need to change the default controller. Initially, in a CodeIgniter application, the default controller is set to
welcome
. However, we don't need that; instead we want the default controller to becreate
. So, consider the following line:$route['default_controller'] = "welcome";
Replace it with the following code:
$route['default_controller'] = "create";
- We will also need to set up a route rule for the
go
controller. We will need to remove the controller and function names (usually the first and second parameters in the URI). The following are two lines of code (highlighted in bold); add these two lines below the404_override
route, so that the file now looks like the following:$route['default_controller'] = "create"; $route['404_override'] = ''; $route['create'] = "create/index"; $route['(:any)'] = "go/index";
Now, the eagle-eyed among you will have looked at that last line and seen the (:any)
type; some of you may have wondered what all that was about.
CodeIgniter supports a simple type of regex that makes routing for unknown URLs much easier. The (:any)
type says to CodeIgniter that any URI pattern not otherwise defined (we're also defining create
) is to be routed to go/index
.
- arc42 by Example
- Hadoop+Spark大數(shù)據(jù)分析實(shí)戰(zhàn)
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- YARN Essentials
- Elasticsearch Server(Third Edition)
- Unity 5 for Android Essentials
- Node.js:來一打 C++ 擴(kuò)展
- 持續(xù)輕量級(jí)Java EE開發(fā):編寫可測(cè)試的代碼
- Couchbase Essentials
- Emgu CV Essentials
- Scratch·愛編程的藝術(shù)家
- 寫給大家看的Midjourney設(shè)計(jì)書
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(第二版)
- 會(huì)當(dāng)凌絕頂:Java開發(fā)修行實(shí)錄
- Bitcoin Essentials