zoukankan      html  css  js  c++  java
  • [PHP]

    前言

    这里使用的是Laravel 5

    PHP Laravel的路由比较强悍,但也正因如此,不统一而容易凌乱。比如在路由中可以直接写方法操作(破坏封装啊)

    以下是个人学习的例子,不供参考


    路由中的直接方法调用和路由地址的使用规则例子

    /**
     * 定义一个/hi地址,返回hi的view
     */
    Route::get('/hi', function()
    {
        return View::make("hi");
    });
    
    /**
     * 定义一个/hello地址,带参数,默认参考值为Robin
     * 地址访问如:/hello/myname
     */
    Route::get("/hello/{name?}", function($name = "Robin"){
        return "Hello " . $name;
    });
    
    /**
     * 定义一个地址:/test/2222
     * 使用正则匹配参数id
     */
    Route::get("/test1/{id}", function($id) {
        return "ID value = " . $id;
    })->where("id", "d+");
    
    /**
     * 定义一个地址:/test2/123/robin
     * 使用正则匹配多个参数
     */
    Route::get("/test2/{id}/{name}", function($id, $name){
        return "ID = " . $id . ", Name = " . $name;
    })->where(["id"=>"d+", "name"=>"[a-zA-Z]+"]);
    
    /**
     * 定义一个/as/my地址,给此路由加一个别名为mm
     */
    Route::get("/as/my", ["as" => "mm", function(){
        // 输入当前路由的名称,如果路由没有给指定别名,返回空值
        return Route::currentRouteName();
    }]);
    
    //------------------------------------------------------------------
    // 测试POST提交
    //------------------------------------------------------------------
    Route::get("/test", function(){
        return View::make("test");
    });
    
    Route::post("/test3", function(){
        // 取得POST的test文本框输入值
        //return $_POST["test"];
        // 取得所有POST的内容
        //return Input::all();
        // 取得指定文本框的输入值
        return Input::get("test");
    });
    
    //------------------------------------------------------------------
    // 测试预定义PID为整型
    //------------------------------------------------------------------
    $router->pattern("pid", "d+");
    Route::get("/test4/{pid}", function($pid) {
        return "Pattern ID = " . $pid;
    });

    当然了,它还有一些什么before、after之些的东西,这里就不写了。

    路由使用Controller例子

    /**
     * 使用Conntroller
     */
    Route::get("/test5", ["as" => "test5", "uses" => "TestController@index"]);
    
    /**
     * 使用Controller重定向
     */
    Route:get("/test6", ["as" => "test6", "uses" => "TestController@test6"]);
    
    /**
     * Route的重定向
     */
    Route::get("test7",function(){
        return Redirect::to("test5");
    });
    
    /**
     * 使用Controller取得当前Route的名称,名称为:mytest8
     */
    Route::get("test8", ["as" => "mytest8", "uses" => "TestController@test8"]);
    
    /**
     * 使用Controller
     * Controller的方法前需要使用get开头
     */
    Route::controller("/my/test", "myMyTestController");
    
    //------------------------------------------------------------------
    // Controller Group
    // 加入前缀my2,意思是在里头的所有路由地址都以my2开头
    // 例如:http://localhost/my/add
    //------------------------------------------------------------------
    Route::group(["prefix" => "my2"], function() {
       Route::controller("/", "myMyTest2Controller");
    });
  • 相关阅读:
    Unique Binary Search Trees 解答
    Unique Paths II 解答
    Unique Paths 解答
    Maximum Subarray 解答
    Climbing Stairs 解答
    House Robber II 解答
    House Robber 解答
    Valid Palindrome 解答
    Container With Most Water 解答
    Remove Duplicates from Sorted List II 解答
  • 原文地址:https://www.cnblogs.com/HD/p/4569804.html
Copyright © 2011-2022 走看看