laravel接口设计

在各种公共方法都设计好,软件安装成功的条件下

routes/web.php中路由信息如下

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/index', 'Api\BannerController@index'); //后台登录操作
View Code

app/Http/ontollers/Api/BannerController.php中代码如下

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;
use Validator;

class   BannerController extends ApiController
{
   public function index(Request $request){
       $validator=Validator::make($request->all(),[
           'token'=>'required|string',
           'type'=>'required|int'
       ],[
               'token.required'=>'token为空',
               'type.required'=>'type为空',
               'type.int'=>'type字段仅支持整形'
           ]
       );
       if($validator->fails()){
return returnJson('400','sb',$validator->errors());
       }
       $info=DB::table('admin')->get();
       return json_encode(['code'=>'10200','message'=>'cg','data'=>$info[0]]);
   }

}
View Code

网址:http://localhost/ay/public/index.php/index?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MX0.eR3VWi_o8n_dMcrzcy3nIEeQ2Rk96hX0O8UjW33JQPM&type=1

 

例子二:

web.php中路由为

Route::any('/test/show','UserController@show');

Controllers/UserController.php中代码为

<?php
/**
 * Created by PhpStorm.
 * User: liuyanjie
 * Date: 2018/12/9
 * Time: 11:48
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function show(Request $request)
    {
        $username=$request->get('username');
        return $username;
    }
}
View Code

访问http://localhost/laravel57/public/index.php//test/show?username=aaa

便可得到如下结果

 

例子三:

web.php中路由为

 Route::any('/test/hello',function (){
     return 'This is a request from any HTTP verb';
 });
View Code

然后访问http://localhost/laravel57/public/index.php//test/hello

便可得到

 

例子四:

web.php中路由为

Route::any('/test/show','UserController@show');

Controllers/UserController.php中代码为

<?php
/**
 * Created by PhpStorm.
 * User: liuyanjie
 * Date: 2018/12/9
 * Time: 11:48
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function show(Request $request)
    {
        $username=$request->get('username');

        return view('index')->with(['info'=>$username]);
    }
}
View Code

view('index')指向 resources/views/index.blade.php  ,向该文件传递 $info这个参数

index.blade.php 中的代码为:

{{ $info }}

这样便可以将参数进行显示。

访问http://localhost/laravel57/public/index.php//test/show?username=aaa

可得到

 

例子五:访问数据库

路由

Route::any('/test/show','UserController@show');

控制器:

<?php
/**
 * Created by PhpStorm.
 * User: liuyanjie
 * Date: 2018/12/9
 * Time: 11:48
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function show(Request $request)
    {
        // $username=$request->get('username');
        $info=DB::table('admin')->get();
        return json_encode(['code'=>'10200','message'=>'cg','data'=>$info[0]]);
    }
}
View Code

 

posted @ 2018-12-02 13:53  JarvisLau  阅读(1315)  评论(0编辑  收藏  举报