Codeigniter更改URL方法名称


Codeigniter Change URL method name

我是CI的新手。

我想将地址栏url中的函数名称从add_car更改为addcar

事实上,我的网址创建如下

http://localhost/projectName/controller/add_car

但我想在URL 下面

http://localhost/projectName/controller/addcar

有可能吗?请帮帮我。

[Note]:我的实际方法名称是add_car

您可以通过两种方法

方法01

编辑-config/routes.php

$route['controller/addcar'] = 'controller/add_car';
$route['controller/deletecar'] = 'controller/delete_car';

输出-www.exapmle.com/controller/addcar


方法02

根据需要更改控制器函数名称。

public function addcar($value='')
{
    # code...
}
public function deletecar($value='')
{
    # code...
}

输出-www.exapmle.com/controller/addcar


进一步了解

如果您使用$route['addcar'] = 'controller/add_car';,URL看起来像

www.exapmle.com/addcar

在控制器中将add_car功能更改为addcar

function add_car(){
  //...
}

function addcar(){
          ^
  //...
}

或在routes.php

$route['controller/add_car'] = "controller/addcar";

$route['controller/([a-z]+)_([a-z]+)'] = "controller/$1$2";

上面的示例将把两个字符串之间包含"_"的每个请求操作路由到不包含"_"的操作/方法。

有关Code Igniter正则表达式路由的详细信息:
https://ellislab.com/codeigniter/user-guide/general/routing.html

你可以在你的路线上使用这个:

$route['addcar'] = 'Add_car/index';
$route['addcar/(:any)'] = 'Add_car/car_lookup/$1';

和你的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add_car extends CI_Controller {
public function __construct()
{
    parent::__construct();
}
public function car_lookup($method = NULL)
{
    if (method_exists($this, $method))
    {
        $this->$method();
    }
    else
    {
        $this->index(); // call default index
    }
}
public function index()
{
    echo "index";
}

public function method_a()
{
    echo "aaaaa";
}
public function method_b()
{
    echo "bbbbb";
}
}