thinkphp 請求

請求對象由 think\\Request 負責 只需要依賴注入即可

請求對象

構造方法注入即可

 public function __construct(Request $request)
{
\t\t$this->request = $request;
}
預覽

請求信息

namespace app\\index\\controller;
use think\\exception\\ValidateException;
use think\\facade\\Request;
class Index extends BaseController
{
/**
* 顯示資源列表
*
* @return \\think\\Response
*/
public function index(Request $request)
{
return Request::url();
}
/**
* 顯示創建資源表單頁.
*
* @return \\think\\Response
*/
public function create()
{
//
}
/**
* 保存新建的資源
*
* @param \\think\\Request $request
* @return \\think\\Response
*/
public function save(Request $request)
{

//
}
/**
* 顯示指定的資源
*
* @param int $id
* @return \\think\\Response
*/
public function read($id)
{
//
}
/**
* 顯示編輯資源表單頁.
*
* @param int $id
* @return \\think\\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的資源
*
* @param \\think\\Request $request
* @param int $id
* @return \\think\\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 刪除指定資源
*
* @param int $id
* @return \\think\\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}

}

輸入網址 http://localhost:8082/ 返回斜槓

輸入變量

需要引入

use think\\facade\\Request;

在中間件裡設置變量,然後使用has進行檢測。

或者可以輸入參數的方式獲取變量

路由設置

use think\\facade\\Route;
Route::rule('ming/:name', 'index/index');

控制器設置

namespace app\\index\\controller;
use think\\exception\\ValidateException;
use think\\facade\\Request;
class Index extends BaseController
{
/**
* 顯示資源列表
*
* @return \\think\\Response
*/
public function index($name)
{
return Request::param('name');
}
/**
* 顯示創建資源表單頁.

*
* @return \\think\\Response
*/
public function create()
{
//
}
/**
* 保存新建的資源
*
* @param \\think\\Request $request
* @return \\think\\Response
*/
public function save(Request $request)
{
//
}
/**
* 顯示指定的資源
*
* @param int $id
* @return \\think\\Response
*/
public function read($id)
{
//
}
/**
* 顯示編輯資源表單頁.
*
* @param int $id
* @return \\think\\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的資源
*
* @param \\think\\Request $request
* @param int $id
* @return \\think\\Response
*/
public function update(Request $request, $id)
{
//

}
/**
* 刪除指定資源
*
* @param int $id
* @return \\think\\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}
}

訪問 url http://localhost:8082/index/ming/45

此時頁面輸出 45

當請求為?的時候,使用get獲取

同樣可以使用助手函數獲取

請求類型

添加 _method 進行偽裝

頭信息

namespace app\\index\\controller;

use think\\exception\\ValidateException;

use think\\facade\\Request;

class Index extends BaseController

{

/**

* 顯示資源列表

*

* @return \\think\\Response

*/

public function index($name)

{

return Request::header('user-agent');

}

/**

* 顯示創建資源表單頁.

*

* @return \\think\\Response

*/

public function create()

{

//

}

/**

* 保存新建的資源

*

* @param \\think\\Request $request

* @return \\think\\Response

*/

public function save(Request $request)

{

//

}

/**

* 顯示指定的資源

*

* @param int $id

* @return \\think\\Response

*/

public function read($id)

{

//

}

/**

*

* @param int $id

* @return \\think\\Response

*/

public function edit($id)

{

//

}

/**

* 保存更新的資源

*

* @param \\think\\Request $request

* @param int $id

* @return \\think\\Response

*/

public function update(Request $request, $id)

{

//

}

/**

* 刪除指定資源

*

* @param int $id

* @return \\think\\Response

*/

public function delete($id)

{

//

}

public function __call($name, $arguments)

{

// TODO: Implement __call() method.

return 'error request';

}

}

輸入 http://localhost:8082/index/ming/name

返回當前的頭信息

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/74.0.3729.169 Chrome/74.0.3729.169 Safari/537.36

偽靜態

配置文件修改偽靜態。。

服務器需要支持.htass文件

// +----------------------------------------------------------------------

// | ThinkPHP [ WE CAN DO IT JUST THINK ]

// +----------------------------------------------------------------------

// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: liu21st <liu21st>

// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

// | 應用設置

// +----------------------------------------------------------------------

return [

// PATHINFO變量名 用於兼容模式

'var_pathinfo' => 's',

// 兼容PATH_INFO獲取

'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],

// pathinfo分隔符

'pathinfo_depr' => '/',

// HTTPS代理標識

'https_agent_name' => '',

// URL偽靜態後綴

'url_html_suffix' => 'html',

// URL普通方式參數 用於自動生成

'url_common_param' => true,

// 是否開啟路由延遲解析

'url_lazy_route' => false,

// 是否強制使用路由

'url_route_must' => false,

// 合併路由規則

'route_rule_merge' => false,

// 路由是否完全匹配

'route_complete_match' => false,

// 使用註解路由

'route_annotation' => false,

// 是否開啟路由緩存

'route_check_cache' => false,

// 路由緩存連接參數

'route_cache_option' => [],

// 路由緩存Key

'route_check_cache_key' => '',

// 訪問控制器層名稱

'controller_layer' => 'controller',

// 空控制器名

'empty_controller' => 'Error',

// 是否使用控制器後綴

'controller_suffix' => false,

// 默認的路由變量規則

'default_route_pattern' => '[\\w\\.]+',

// 域名根,如thinkphp.cn

'url_domain_root' => '',

// 是否自動轉換URL中的控制器和操作名

'url_convert' => true,

// 表單請求類型偽裝變量

'var_method' => '_method',

// 表單ajax偽裝變量

'var_ajax' => '_ajax',

// 表單pjax偽裝變量

'var_pjax' => '_pjax',

// 是否開啟請求緩存 true自動緩存 支持設置請求緩存規則

'request_cache' => false,

// 請求緩存有效期

'request_cache_expire' => null,

// 全局請求緩存排除規則

'request_cache_except' => [],

// 默認控制器名

'default_controller' => 'Index',

// 默認操作名

'default_action' => 'index',

// 操作方法後綴

'action_suffix' => '',

// 默認JSONP格式返回的處理方法

'default_jsonp_handler' => 'jsonpReturn',

// 默認JSONP處理方法

'var_jsonp_handler' => 'callback',

];

連接如下

http://localhost:8082/index.php/index/ming/name.html

此時訪問的等價於

http://localhost:8082/index.php/index/ming/name

參數綁定

這個連接

http://localhost:8082/index/ming/year/34/month/34

定義路由

use think\\facade\\Route;

Route::rule('ming/year/:year/month/<month>', 'index/index');/<month>

其中month為可選

控制器如下

namespace app\\index\\controller;

use think\\exception\\ValidateException;

use think\\facade\\Request;

class Index extends BaseController

{

/**

* 顯示資源列表

*

* @return \\think\\Response

*/

public function index($year, $month = '01')

{

return $year . $month;

}

/**

* 顯示創建資源表單頁.

*

* @return \\think\\Response

*/

public function create()

{

//

}

/**

* 保存新建的資源

*

* @param \\think\\Request $request

* @return \\think\\Response

*/

public function save(Request $request)

{

//

}

/**

* 顯示指定的資源

*

* @param int $id

* @return \\think\\Response

*/

public function read($id)

{

//

}

/**

*

* @param int $id

* @return \\think\\Response

*/

public function edit($id)

{

//

}

/**

* 保存更新的資源

*

* @param \\think\\Request $request

* @param int $id

* @return \\think\\Response

*/

public function update(Request $request, $id)

{

//

}

/**

* 刪除指定資源

*

* @param int $id

* @return \\think\\Response

*/

public function delete($id)

{

//

}

public function __call($name, $arguments)

{

// TODO: Implement __call() method.

return 'error request';

}

}

此時輸入

http://localhost:8082/index/ming/year/34/month/34

返回

3434

請求緩存

將會對請求進行緩存

點擊查看更多內容


分享到:


相關文章: