简体   繁体   中英

Codeigniter Change URL method name

I am new in CI.

I want to change function name in addressbar url with add_car to addcar .

Actually my url is created as below

http://localhost/projectName/controller/add_car

But I want following in URL

http://localhost/projectName/controller/addcar

Is it possible? Please help me.

[Note] : My actual method name is add_car .

You can do it by two methods

Method 01

Edit - config/routes.php

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

output - www.exapmle.com/controller/addcar


Method 02

change your controller function name as you like.

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

Output - www.exapmle.com/controller/addcar


Further Knowledge

If you use $route['addcar'] = 'controller/add_car'; URL looks like

www.exapmle.com/addcar

Change add_car function to addcar in your controller

function add_car(){
  //...
}

To

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

Or in routes.php

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

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

Above example will route every requested action containing '_' between two strings to action/method without the '_'.

More about Code Igniter regular expression routes:
https://ellislab.com/codeigniter/user-guide/general/routing.html

You can use this on your route:

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

and your controller

<?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";
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM