官术网_书友最值得收藏!

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:

  1. 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'] = '';
  2. 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 be create. So, consider the following line:
    $route['default_controller'] = "welcome";

    Replace it with the following code:

    $route['default_controller'] = "create";
  3. 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 the 404_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.

主站蜘蛛池模板: 宣武区| 定襄县| 竹溪县| 乌恰县| 岑巩县| 昌图县| 临朐县| 永登县| 屯门区| 鄂托克前旗| 仁怀市| 婺源县| 博白县| 揭阳市| 申扎县| 广宗县| 建湖县| 万安县| 繁昌县| 宜丰县| 城市| 左云县| 建平县| 张掖市| 雷山县| 龙口市| 句容市| 海伦市| 株洲县| 当涂县| 抚顺市| 波密县| 邹城市| 游戏| 常德市| 珲春市| 黔西| 乐清市| 若尔盖县| 巨鹿县| 青阳县|