1.环境要求安装LNMP开发环境,推荐使用宝塔
2.composer global require “laravel/installer”
3.如果缺少vendor文件,使用composer install
4.应用程序生成秘钥 php artisan key:generate
5,本教程基于laravel5.5
复制项目目录下的配置文件 cp .env.example .env
修改 .env文件
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=username
DB_PASSWORD=password
vim routes/web.php
vim app/Http/Controllers/controller.php
use Illuminate\Http\Request;
class Controller extends BaseController
{
/**添加参数校验
*测试get请求
* @param $name
*/
public function index(Request $request, $name)
{
$env = App::environment(); //环境变量
$res = ['name'=>$name];
return response()->json($res);
}
}
postman进行测试
vim routes/web.php
//测试postAPI参数
Route::post('test', 'Controller@test');
vim app/Http/Controllers/controller.php
use Illuminate\Http\Request;
class Controller extends BaseController
{
/**
* 测试post请求
* @param $name $age
*/
public function test(Request $request)
{
$name = $request->input('name');
$age = $request->input('age');
$result['name'] = $name . $age;
return response()->json($result);
}
}
postman请求测试
新建文件夹 /app/Models
创建model文件 php artisan make:models Test
vim /app/Models/Test.php
use Illuminate\Database\Eloquent\Model;
use DB;
class Test extends Model
{
/**
* 测试方法调用
* model 层数据库调用
*/
public function getData()
{
//原生sql操作
$users = DB::select('select * from user', [1]);
//ORM
$data = DB::table('user')->get()->getList();//对象转为数组
$res = $data[0]['id'];
return $res;
}
vim app/Http/Controllers/Controller.php
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Test;//测试model类
use Predis;
class Controller extends BaseController
{
public function test()
{
$Test = new Test; //初始化类
$res['data'] = $Test->getData();
return response()->json($res);
}
}
laravel官方推荐使用composer安装predis
本示例使用phpredis(c扩展)
修改配置文件config/database.php
修改redis别名config/app.php
'redis' => [
'client' => 'phpredis', //由pedis修改为phpredis
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
'read_timeout' => 60,
],
],
//'Redis' => Illuminate\Support\Facades\Redis::class,
'Predis' => Illuminate\Support\Facades\Redis::class,
测试redis连接
use Predis;
class Controller extends BaseController
{
/**
* 测试redis连接
* @param $name $age
*/
public function test()
{
$data = Predis::connection();
var_dump($data);
}
}
添加配置文件
vim config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mysql_center' => [
'driver' => 'mysql',
'host' => env('DB_HOST_CENTER', '127.0.0.1'),
'port' => env('DB_PORT_CENTER', '3306'),
'database' => env('DB_DATABASE_CENTER', ''),
'username' => env('DB_USERNAME_CENTER', ''),
'password' => env('DB_PASSWORD_CENTER', ''),
'unix_socket' => env('DB_SOCKET_CENTER', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
vim .env
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=name
DB_PASSWORD=password
DB_HOST_CENTER=127.0.0.1
DB_PORT_CENTER=3306
DB_DATABASE_CENTER=test
DB_USERNAME_CENTER=name
DB_PASSWORD_CENTER=password
测试连接
vim app/Http/Controllers/controller.php
use Illuminate\Http\Request;
use DB;
class Controller extends BaseController
{
/**添加参数校验
*测试get请求
* @param $name
*/
public function index(Request $request, $name)
{
$dataDafault = DB::select('select * from user', [1]);
echo '使用默认数据库查询';
var_dump($dataDafault);
echo '使用mysql_center查询';
$dataCenter = DB::connection('mysql_center')->select('select * from user', [1]);
var_dump($dataCenter);
}
}