zoukankan      html  css  js  c++  java
  • 2.控制器 php artisan make:controller 控制器名 (大驼峰)+Controller

    1.控制器生成:

    php artisan make:controller TestController

    <?php

    namespace AppHttpControllers;

    use IlluminateHttpRequest;//命名空间的三元素:常量,方法和类

    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:phpStudyPHPTutorialWWWlaravel02>php artisan make:controller Home/IndexController

    E:phpStudyPHPTutorialWWWlaravel02>php artisan make:controller Admin/IndexController

    //home目录 index 类的index方法
    Route::get('/home/index/index','HomeIndexController@index');
    Route::get('/admin/index/index','AdminIndexController@index');
    http://www.laravel02.com/home/index/index
    http://www.laravel02.com/admin/index/index

    4. 接收用户输入

    接收用户输入的类:IlluminateSupportFacadesInput

      Facades 是类的一个接口实现,算静态方法

    Input::get();

    Input::all();

    input::only([])

    input::except([])

    简化 use IlluminateSupportFacadesInput ,给它添加别名

    测试:
    Route::get('/home/test/test2','TestController@test2');
    http://www.laravel02.com/home/test/test2?ddd=aaaa&id=112&name=zhangsan
    <?php

    namespace AppHttpControllers;

    use IlluminateHttpRequest;//命名空间的三元素:常量,方法和类
    //use IlluminateSupportFacadesInput;
    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"
    ]



  • 相关阅读:
    day⑥:logging模块
    day⑥:shelve模块
    day⑥:xml模块
    day⑤:冒泡排序
    day⑤:模块
    day⑤:re深入
    day④:递归
    day④:迭代器
    day④:装饰器
    day③:函数式编程
  • 原文地址:https://www.cnblogs.com/alex-13/p/13730710.html
Copyright © 2011-2022 走看看