-
路径:/App/Config/route.php
-
规则:
<?php
return [
"URL" => [
"Method" => "Controller@Function",
"Method2" => "Controller2@Function2"
],
"URL2" => [
"Method3" => "Controller3@Function3",
"Method4" => "Controller4@Function4"
]
];
<?php
return [
"/user" => [
"GET" => "User@getList"
]
];
- /user 表示访问 http://YourDomain/user 。
- GET 表示Get方法访问该接口。
- User@getList 表示访问的是
/App/Controllers/User.php
中的getList
方法。
<?php
namespace App\Controllers;
class User
{
public function getList()
{
echo "hello";
}
}
- Get方法访问http://YourDomain/user 将会返回
hello
。
<?php
return [
"/user" => [
"GET" => "User@getList",
"DELETE" => "User@delete"
]
];
- 如果为 Get 方法访问该接口将会访问 getList 方法。
- 如果为 DELETE 方法访问该接口将会访问 delete 方法。
<?php
return [
"/user/{id}" => [
"DELETE" => "Admin/User@delete"
]
];
<?php
namespace App\Controllers\Admin;
class User
{
public function delete($id)
{
echo $id;
}
}
- DELETE 方法访问 http://YourDomain/user/123 返回值
123
。
<?php
return [
"/user/{id}" => [
"DELETE" => function ($id) {
echo $id;
}
]
];
- DELETE 方法访问 http://www.domain.com/user/123 返回值
123
。
<?php
return [
"/student" => [
"GET" => "Student@getList",
"POST" => "Admin@addStudent"
],
"/user/{id}/name/{nickname}" => [
"GET" => "Student@getInfo",
"PUT" => "Admin@changeStudentInfo"
]
];
- 控制器 /App/Controllers/Student.php
<?php
namespace App\Controllers;
class Student
{
public function getList()
{
echo "StudentList";
}
public function getInfo($id, $nickname)
{
echo "ID:" . $id;
echo "Name:" . $nickname;
}
}
- 控制器 /App/Controllers/Admin.php
<?php
namespace App\Controllers;
class Admin
{
public function addStudent()
{
echo "This is addStudent";
}
public function changeStudentInfo($id, $nickname, $newName)
{
echo "ID:" . $id;
echo "Name:" . $nickname;
echo "newName:" . $newName;
}
}
Get访问,返回值:
StudentList
Post访问,返回值:
This is addStudent
Get访问,返回值:
ID:123 Name:Zereri
Post带参数newName=ZZZ访问,返回值:
ID:123 Name:Zereri newName:ZZZ
- 修改config.php
'version_control' => true
<?php
return [
"v1" => [
"/api/list" => [
"GET" => "Api@index"
],
"URL" => [
"Method" => "Controller@Function"
]
],
"v2" => [
"/api/list" => [
"GET" => "First@hello"
]
]
];
- 访问 http://YourDomain/v1/api/list 访问
/App/Controllers/Api.php
的index
方法 - 访问 http://YourDomain/v2/api/list 访问
/App/Controllers/First.php
的hello
方法