1.控制器生成:
php artisan make:controller TestController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;//命名空間的三元素:常量,方法和類
class TestController extends Controller
{
public function test1(){
phpinfo();
}
}
2.控制器路由:
使用路由規則調用控制器下的方法,非回調函數
“控制器類名@方法名”
// 實戰測試
Route::get('/home/test/test1','TestController@test1');
http://www.laravel02.com/home/test/test1
3. 分目錄管理, 命令里增加目錄名即可:
E:\phpStudy\PHPTutorial\WWW\laravel02>php artisan make:controller Home/IndexController
E:\phpStudy\PHPTutorial\WWW\laravel02>php artisan make:controller Admin/IndexController
//home目錄 index 類的index方法
Route::get('/home/index/index','Home\IndexController@index');
Route::get('/admin/index/index','Admin\IndexController@index');
http://www.laravel02.com/home/index/index
http://www.laravel02.com/admin/index/index
4. 接收用戶輸入
接收用戶輸入的類:Illuminate\Support\Facades\Input
Facades 是類的一個接口實現,算靜態方法
Input::get();
Input::all();
input::only([])
input::except([])
簡化 use Illuminate\Support\Facades\Input ,給它添加別名
測試:
Route::get('/home/test/test2','TestController@test2');
http://www.laravel02.com/home/test/test2?ddd=aaaa&id=112&name=zhangsan
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;//命名空間的三元素:常量,方法和類
//use Illuminate\Support\Facades\Input;
use Input;
class TestController extends Controller
{
public function test1(){
phpinfo();
}
//測試input
public function test2(){
//獲取一個值,如果沒值默認第二個參數
echo Input::get('id',"10086");
//獲取全部(數組格式)
$all = Input::all();
//dump + die ,后續代碼不會執行
// dd($all);
//獲取指定信息(字符串格式)
// dd(Input::get('name'));
//獲取指定幾個key(數組格式)
// dd(Input::only(['id','name']));
//獲取指定幾個key之外的值(數組格式)
// dd(Input::except(['name']));
//判斷key是否存在(boole)
dd(Input::has('name'));
}
}
輸出:
112
array:1 [▼ "id" => "112" ]