1、 composer create-project --prefer-dist laravel/lumen xfproject
2、项目的nginx虚拟主机配置
==========不含https============
server {
listen 80;
server_name test.xfproject.com;
root '/Users/xiafen/web/xfproject/public';
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
==========含https的复杂配置============
server {
listen 80;
server_name test.xfproject.com;
listen 443 ssl;
ssl_certificate cert/xfproject.pem;
ssl_certificate_key cert/xfproject.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
index index.php index.html index.htm;
set $root_path '/Users/xiafen/web/xfproject/';
set $web_path 'public';
set $assets_path 'packages/woldy/cms/src';
root $root_path$web_path;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /assets/woldycms/{
rewrite ^/assets/woldycms/(.*)$ /assets/$1 break;
root $root_path$assets_path;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}2、到项目根目录/vendor/laravel/lumen-framework下面把config文件夹copy一份,放在项目根目录下
3、在项目根目录/app/新建Service文件夹,里面可以封装一个工厂设计模式,这里可以封装根据不同的参数连接不同的数据库
千万注意:一定要创建的class类php文件而不是普通php文件哈!!!
==========ServiceFactory.php
namespace App\Service;
class ServiceFactory{
private static $instance = [];
private static $_prefix = "Service";
public static function factory($class_name){
$class_name = '\App\Service\\'.$class_name.self::$_prefix;
if (!isset(self::$instance[$class_name])) {
self::$instance[$class_name] = new $class_name();
}
return self::$instance[$class_name];
}
}
==========TestAkuService.php
namespace App\Service;
class TestAkuService{
public function testAsql($id){
$sql="select * from table_a where id='".$id."'";
return $sql;
}
}
==========TestBkuService.php
namespace App\Service;
class TestBkuService{
public function testBsql($id){
$sql="select * from table_b where id='".$id."'";
return $sql;
}
}
//某个控制方法里调用
//1、在routes/web.php 文件里写入路由:$router->get('index1', 'TestController@index1');
//2、在指定的控制方法里调用:
namespace App\Http\Controllers;
use App\Service\ServiceFactory;
class TestController extends Controller{
public function index1(){
$user1=ServiceFactory::factory('TestAku')->testAsql(18);
$user2=ServiceFactory::factory('TestBku')->testBsql(12);
echo $user1;echo "
";echo $user2;
}
}
// 3、输出结果:
// select * from table_a where id='18'
// select * from table_b where id='12'
=========================================================================
4、安装外部依赖库vendor
进入命令行,cd到该项目根目录,composer install
5、存储文件的创建
(1)缓存
cd storage/framework, 里面应该有三个文件夹:cache-session-views, 新创建的项目少了session文件夹,手动创建即可 mkdir -p session
php artisan 显示列表,我们php artisan cache:table
(2)项目日志
cd storage/logs,创建项目日志文件并加上权限:
touch lumen.log
chmod 777 lumen.log
6、session的使用
https://www.jianshu.com/p/dc33f8ab0618
(1)配置session(项目根目录/config/session.php + 项目根目录/.env)和配置session存储
配置session
项目根目录/config/session.php + 项目根目录/.env,优先使用.env文件的配置,.env里没有的,走项目根目录/config/session.php里的配置。
.env文件里没有的走config/session.php下的配置。
//session.php
return [
'driver' => env('SESSION_DRIVER', 'file'),//默认使用file驱动,你可以在.env中配置
'lifetime' => 120,//缓存失效时间
'expire_on_close' => false,
'encrypt' => false,
'files' => storage_path('framework/session'),//file缓存保存路径
'connection' => null,
'table' => 'sessions',
'lottery' => [2, 100],
'cookie' => 'laravel_session',
'path' => '/',
'domain' => null,
'secure' => false,
];
//.env
SESSION_DRIVER=file
配置session存储
session一般采用file来存储。命令行进入 项目根目录/storage/framework/下,mkdir -p session.php
(2)在 项目根目录/bootstrap/app.php文件里面加入:
// 1、注册 SessionServiceProvider
$app->register(Illuminate\Session\SessionServiceProvider::class);
// 2、载入session相关配置
$app->configure('session');//加载 项目根目录/config/session.php文件
// 3、设置session别名,这个和Illuminate\Support\Facades\Session.php里面的getFacadeAccessor方法里的session必须一样,都是小写
//问:和后面的那串路径咋来的?==>控制器-方法里,session::put(...),鼠标点击跳转到这个路径的!
$app->alias('session', 'Illuminate\Session\SessionManager');
// 4、注册StartSession中间件
$app->middleware([
Illuminate\Session\Middleware\StartSession::class
]);
(3)控制器-方法里调用session
$router->get('put_session', 'Home\HuancunController@put_session');
$router->get('get_session', 'Home\HuancunController@get_session');
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class HuancunController extends Controller
{
public function put_session(Request $request){
echo "put
";
app('session')->put('user','llkkjj');
//app('session')->put('user','aaabbbccc');
}
public function get_session(Request $request){
echo "get
";
$user=session1::get('user');
if(!empty($user)){
echo $user;
}else{
echo "未设置session";
}
}
}
6.2 cache的使用
(1)config/cache.php 和 .env的配置
config/cache.php
return [
'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
'apc' => [
'driver' => 'apc',
],
'array' => [
'driver' => 'array',
],
'database' => [
'driver' => 'database',
'table' => env('CACHE_DATABASE_TABLE', 'cache'),
'connection' => env('CACHE_DATABASE_CONNECTION', null),
],
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
],
'memcached' => [
'driver' => 'memcached',
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'), 'port' => env('MEMCACHED_PORT', 11211), 'weight' => 100,
],
],
],
'redis' => [
'driver' => 'redis',
'connection' => env('CACHE_REDIS_CONNECTION', 'default'),
],
],
'prefix' => env('CACHE_PREFIX', 'laravel'),
];
//.env
CACHE_DRIVER=file
(2)bootstrap/app.php 去掉 $app->withFacades(); 前面的注释。
(3)直接在 控制器-方法 调用
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class HuancunController extends Controller
{
public function put_cache(Request $request){
Cache::put('name','xiaofang',300);//注意要加过期时间
echo "put_cache成功
";
}
public function get_cache(Request $request){
$name=Cache::get('name');
if(!empty($name)){
echo "get_cache成功=";echo $name;
}else{
echo "未设置cache";
}
}
}
6.3 redis的配置
https://blog.csdn.net/Dennis_ukagaka/article/details/76849573
1、安装redis扩展
在 项目根目录/composer.json 的require里面加上这2行
"predis/predis": "^1.1",
"illuminate/redis": "^5.6"
然后到命令行,cd到项目根目录下
composer require predis/predis
composer require illuminate/redis
2、bootstrap/app.php中要引入redis的扩展
$app->register(Illuminate\Redis\RedisServiceProvider::class);
并且在该app.php文件下,’Facades’和’Eloquent’的$app->withFacades() 和 $app->withEloquent()注释打开
3、在config/database.php文件和.env文件中,可以全局配置各类参数
(1)config/database.php文件:见第6.5的(1)
(2).env文件
REDIS_HOST=192.168.1.41
REDIS_PORT=7000
REDIS_PASSWORD=123456
4、(1)用redis作为cache缓存
.env里面,将cache缓存方式改成redis,然后就可以正常使用 cache::put 和 cache::get
CACHE_DRIVER=redis
(2)控制器方法中使用redis
//第一种用法
use Illuminate\Support\Facades\Redis
Redis::setex('site_name', 10, 'Lumen的redis');
return Redis::get('site_name');
//第二种用法
use Illuminate\Support\Facades\Redis
Illuminate\Support\Facades\Cache
Cache::store('redis')->put('site_name', 'Lumen测试', 10);
return Cache::store('redis')->get('site_name');
6.4 memcache的配置
https://blog.csdn.net/cup_chenyubo/article/details/49682425
下载memcache的地址:curl -O http://pecl.php.net/get/memcache-3.0.8.tgz
6.5 database的配置
(1)config/database.php 和 .env
//config/database.php
return [
'fetch' => PDO::FETCH_CLASS,
/*Default Database Connection Name*/
/*注意这里connection连的是数据库名字!!而不是类型!!这一系列名字列表里的driver字段值才是类型*/
'default' => env('DB_CONNECTION', 'mysql'),//默认连接数据库的名字是mysql
/*Database Connections*/
'connections' => [
'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
],
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', base_path('database/database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => env('DB_PREFIX', ''),
'timezone' => env('DB_TIMEZONE', '+00:00'),
'strict' => env('DB_STRICT_MODE', false),
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 5432),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => env('DB_PREFIX', ''),
'schema' => env('DB_SCHEMA', 'public'),
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => env('DB_PREFIX', ''),
],
//自定义数据库
'zhiyinlou' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'zhiyinlou'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => env('DB_CHARSET', 'utf8'),
'prefix' => env('DB_PREFIX', ''),
],
],
/*Migration Repository Table*/
'migrations' => 'migrations',
/* Redis Databases*/
'redis' => [
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
'password' => env('REDIS_PASSWORD', null),
],
],
];
//.env
//如果一个数据库//
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=123456
//如果多个数据库//
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test1
DB_USERNAME=root
DB_PASSWORD=123456
DB_PORT_APP=3307
DB_DATABASE_APP=test2
DB_USERNAME_APP=root
DB_PASSWORD_APP=123456
DB_PORT_SERVICE=3308
DB_DATABASE_SERVICE=test3
DB_USERNAME_SERVICE=root
DB_PASSWORD_SERVICE=123456
(2)在 项目根目录下创建Models文件夹,新建一个class类php文件
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class TestModel
extends Model {
protected $connection = 'zhiyinlou';//如果有多个数据库,这里要指明此处的数据库,只有1个的话,此句可省略
public $table = 'feedback';
public $timestamps = true;
protected $guarded =[];
}
(3)控制器-方法里面调用
$router->get('test', 'Home\HuancunController@test');
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\TestModel;
class HuancunController extends Controller{
public function test(Request $request){
$list=TestModel::select('*')->get()->toArray();
var_dump($list);die;
}
}
7、自定义全局可用的变量
(1)首先:在config文件夹下新建文件,如test.php
return [
'zhangsan'=>env('ZHANGSAN','good-student'),
'key_name'=>env('KEY_NMAE','yangwang')
];
其次:在.env文件里可以去全局定义的
ZHANGSAN=good-worker
最后:test.php注册到bootstrap/app.php文件里
$app->configure('test');
(2)该项目的任何地方都调用,调用方法:config(‘文件名.变量名’)
用之前最好先用tinker调用变量测试看看结果(tinker的安装使用见第9条)
php artisan tinker
config('test.zhangsan') //good-worker
config('test.key_name') //yangwang
如果改动了全局变量的值,可以退出artisan,再次php artisan tinker
此时config('文件名.变量名') 就是改后的了
8、自定义全局可用的函数
在 项目根目录/bootstrap文件夹下建立helper.php,想全局使用的函数都可以塞进这里。
然后在 项目根目录/bootstrap/app.php文件里加入它: require_once __DIR__ . '/helpers.php';
=====================
if (!function_exists('is_phone_number')) {
/**
* 判断是否为手机号
*
* @param $value
* @return bool
*/
function is_phone_number($value)
{
return !!preg_match('/^1[3456789]\d{9}$/', $value);
}
}
if (!function_exists('is_email_address')) {
/**
* 判断是否为邮箱
*
* @param $value
* @return bool
*/
function is_email_address($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
}
}
if (!function_exists('is_id_number')) {
/**
* 判断是否为身份证号
*
* @param $idNumber
* @return bool
*/
function is_id_number($idNumber)
{
$idNumber = strtoupper($idNumber);
$pattern = '/^\d{17}([0-9]|X)$/';
if (!preg_match($pattern, $idNumber)) {
return false;
}
$matches = array();
$pattern = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/';
@preg_match($pattern, $idNumber, $matches);
// 检查生日日期是否正确
$birthday = $matches[2] . '/' . $matches[3] . '/' . $matches[4];
if (strtotime($birthday)) {
// 检验18位身份证的校验码是否正确。
// 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
$map = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$sum = 0;
for ($i = 17; $i > 0; $i--) {
$n = pow(2, $i) % 11;
$sum += $n * (int) $idNumber[17 - $i];
}
return $map[$sum % 11] === $idNumber[17];
}
return false;
}
}
if (!function_exists('generate_random_string')) {
/**
* 生成指定长度的随机字符串
* @param null $characters
* @param int $length
* @return string
*/
function generate_random_string($characters = null, $length = 8)
{
if (is_null($characters)) {
$lowercase = 'abcdefghjkmnpqrstuvwxyz';
$uppercase = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
$digits = '23456789';
$characters = $lowercase . $uppercase . $digits;
}
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
if (!function_exists('generate_random_digits')) {
/**
* 生成指定长度的随机数字字符串
*
* @param null $digits
* @param int $length
* @return string
*/
function generate_random_digits($digits = null, $length = 6)
{
if (is_null($digits)) {
$digits = '0123456789';
}
return generate_random_string($digits, $length);
}
}
if (!function_exists('clac_time_diff')) {
/**
* 计算两个时间的间隔
*
* @param null $digits
* @param int $length
* @return string
*/
function clac_time_diff($startdate, $enddate)
{
$date = floor((strtotime($enddate) - strtotime($startdate)) / 86400);
$hour = floor((strtotime($enddate) - strtotime($startdate)) % 86400 / 3600);
$minute = floor((strtotime($enddate) - strtotime($startdate)) % (86400 * 3600) / 60);
$second = floor((strtotime($enddate) - strtotime($startdate)) % (86400 * 3600 * 60) % 60);
return $date . '天' . $hour . '小时' . $minute . '分' . $second . '秒';
}
}
if (!function_exists('api_response')) {
/**
* API Response Wrapper.
*
* @param $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
function api_response($data = [], $status = 200, array $headers = [], $options = 0)
{
$now = microtime(true);
$data = [
'data' => $data,
'meta' => [
'timestamp' => $now,
'response_time' => $now - LARAVEL_START,
],
];
return response()->json($data, $status, $headers, $options);
}
}
if (!function_exists('allows_any')) {
/**
* Determine if the given any abilities should be granted for the current user
*
* @param string|array $abilities
* @param array $arguments
* @return bool
*/
function allows_any($abilities, $arguments = []) {
foreach ((array) $abilities as $ability) {
if (Gate::allows($ability, $arguments)) {
return true;
}
}
return false;
}
}
if (!function_exists('allows_all')) {
/**
* Determine if the given all abilities should be granted for the current user
*
* @param string|array $abilities
* @param array $arguments
* @return bool
*/
function allows_all($abilities, $arguments = []) {
foreach ((array) $abilities as $ability) {
if (!Gate::allows($ability, $arguments)) {
return false;
}
}
return true;
}
}
if (!function_exists('auto_rename')) {
/**
* Auto rename new filename.
*
* when it taken in list, auto increment new name, likes file.txt, file (1).txt
*
* @param $name
* @param array $collect
* @return string
*/
function auto_rename($name, $collect = []) {
$pieces = pathinfo($name);
$filename = $pieces['filename'];
$extension = $pieces['extension'] ? '.' . $pieces['extension'] : '';
$i = 0;
while (1) {
if (in_array($name, $collect)) {
$name = $filename . ' (' . ++$i . ')' . $extension;
} else {
break;
}
}
return $name;
}
}
if (! function_exists('datetime_format')) {
/**
* 格式化输出日期/时间
*
* @param DateTime|integer|string $dateTime
* @param string $format
* @return false|string
*/
function datetime_format($dateTime, $format = 'Y-m-d H:i:s') {
if ($dateTime instanceof \DateTime) {
return $dateTime->format($format);
}
if (is_numeric($dateTime)) {
return date($format, $dateTime);
}
return date($format, strtotime($dateTime));
}
}
if (!function_exists('html_error_response')) {
/**
* API Response Wrapper.
*
* @param $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
function html_error_response($data = [], $status = 500, array $headers = [])
{
$now = microtime(true);
$data = [
'data' => $data,
'meta' => [
'timestamp' => $now,
'response_time' => $now - LARAVEL_START,
],
];
return response()->view('error_file',$data,$status,$headers);
}
}
if (!function_exists('transform_to_array')){
/**
* 处理对象 转换为数组
* @param $object
* @return array
*/
function transform_to_array($object)
{
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object) && property_exists($object, '_') && count(get_object_vars($object)) === 1) {
return $object->_;
}
$arrays = [];
foreach ($object as $key => $value) {
if (is_object($value) || is_array($value)) {
$arrays[$key] = transform_to_array($value);
} else {
$arrays[$key] = $value;
}
}
return $arrays;
}
}
if(!function_exists('get')){
/**
* 获取数组中某个键对应的值
* @param array $array
* @param $key
* @param $default
* @return mixed
*/
function get(array $array, $key, $default)
{
if (isset($array[$key])) {
return $array[$key];
} else {
return $default;
}
}
}
if(!function_exists('array_column')){
/**
* 获取数组中某一列
* @param array $array
* @param $columnName
* @return array
*/
function column(array $array, $columnName)
{
if (function_exists('array_column')) {
$column = array_column($array, $columnName);
return $column;
}
if (empty($array)) {
return array();
}
$column = array();
foreach ($array as $item) {
if (isset($item[$columnName])) {
$column[] = $item[$columnName];
}
}
return $column;
}
}
if(!function_exists('array_index')){
/**
* 更改数组组件的值
* @param array $array
* @param $name
* @return array
*/
function index(array $array, $name)
{
$indexedArray = array();
if (empty($array)) {
return $indexedArray;
}
foreach ($array as $item) {
if (isset($item[$name])) {
$indexedArray[$item[$name]] = $item;
continue;
}
}
return $indexedArray;
}
}
if(!function_exists('array_keys')){
/**
* 获取数组中某些键对应的值
* @param array $array
* @param array $keys
* @return array
*/
function parts(array $array, array $keys)
{
foreach (array_keys($array) as $key) {
if (!in_array($key, $keys)) {
unset($array[$key]);
}
}
return $array;
}
}
if(!function_exists('array_group')){
/**
* 按某个键分组
* @param array $array
* @param $key
* @return array
*/
function group(array $array, $key)
{
$grouped = array();
foreach ($array as $item) {
if (empty($grouped[$item[$key]])) {
$grouped[$item[$key]] = array();
}
$grouped[$item[$key]][] = $item;
}
return $grouped;
}
}
if(!function_exists('array_required')){
/**
* 判断数组中是否有没有指定的键
* @param array $array
* @param array $keys
* @param bool $strictMode
* @return bool
*/
function required(array $array, array $keys, $strictMode = false)
{
foreach ($keys as $key) {
if (!array_key_exists($key, $array)) {
return false;
}
if ($strictMode && (is_null($array[$key]) || $array[$key] === "" || $array[$key] === 0)) {
return false;
}
}
return true;
}
}
//...继续往后加
=====================
9、laravel/Lumen中tinker(单元测试,或者叫单独测试)的使用
下载tinker服务———测试服务,脚本等等
https://packagist.org/packages/laravel/tinker
按照此教程
(1)composer.json的"require”里加上"laravel/tinker": "^1.0"
(2)进入项目根目录,安装thinker
composer require laravel/tinker
(3)在服务入口文件bootstrap/app.php文件里注册,app.php的都是全局可以用的!
'providers' => [
// Other service providers...
Laravel\Tinker\TinkerServiceProvider::class,
],
(4)使用:进入项目根目录,执行
php artisan tinker
假设有1个数据表模型,现在可以用tinker测试一下
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class MenuModel extends Model
{
protected $connection = 'testdb';
public $table = 'user';
public $timestamps = true;
protected $guarded =[];
}
?>
php artisan tinker
use App\Models\MenuModel
MenuModel::query()->first() //从数据库中取出了一条数据
MenuModel::query()->where('id',1)->first() //也可以使用where条件进行查询,是不是很棒
10、/项目根目录/routes/web.php 里路由的写法
(1)直接在路由里写方法
$router->get('/version', function () use ($router) {
return $router->app->version();
});
(2)指定的控制器-方法
$router->get('index1', 'TestController@index1');
(3)组合带中间件的控制器-方法
$router->group(['prefix' => '/list', 'middleware' => ['authAccess','header']], function () use ($router) {
$router->get('applist', ['uses' => 'Home\IndexController@applist']);
$router->get('feedback', ['uses' => 'Home\IndexController@feedback']);
});
(4)方法有3种:post-get-any(post或get都可以)
$router->get
$router->post
$router->any
11、抛异常为何在Exception前面加\,Exception类好像并没有这个类的php文件啊?
异常是php内置的类,并不在单独的一个class Exception类中,如果不加\,会在当前命名空间下找,这肯定找不到,
加\表示在根命名空间里去找,你可以把鼠标单击\Exception 上,就能跳转到对应的代码()
public function testErr($params){
try {
$parameter = array('a1'=>'b1','a2'=>'b2','a3'=>'b3');
$result = $this->myfunc($parameter);//随便一个自定义函数myfunc,返回的是对象
//调用结果返回异常
if (!$result->out instanceof \stdClass) {
throw new \Exception("调用getCCWorkflowRequestList结果出现异常:".json_encode($result));
}
return $result->out;
} catch (\SoapFault $soapFault) {
throw new \Exception($soapFault->getMessage() . $this->getSoapClientHandler()->__getLastResponse());
}
}
12、加密解密