Q_A.md
6.03 KB
一些Laravel中的代码说明
1.在Controller中函数参数使用Request
首先需要引用Illuminate\Http\Request命名空间,才能使用$request变量
use Illuminate\Http\Request
use dump;
class ProjectController extends Controller
{
public function index(Request $request) {
dump($request);
}
}
也可以在bootstrap/app.php中使用class_alias将Illuminate\Http\Request定义为Request.
// in bootstrap/app.php
class_alias('Illuminate\Http\Request','Request');
// in controller.php
use Request;
public function index(Request $request) {...}
2.Request操作
- 验证
$this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required',
]);
- 获得请求字段
- 方式1:
$order_state = Request::input('order_state');
$evaluation_state = Request::input('evaluation_state');
$orderId = $request->input('order_id',1);
- 方式2:
$request->only('email', 'password');
3.Response操作
Response默认使用的类为 Illuminate\Http\Response
当然,也可以实现一个自定义的Response,作为ServiceProvider,注册到容器中。
具体自定义Response,可以查看Lumen-Api这本书。
- 基本响应
return (new Response($content, $status))
->header('Content-Type', $value);
return response($content, $status)
->header('Content-Type', $value);
- 标头定义响应
return response($content)
->header('Content-Type', $type)
->header('X-Header-One', 'Header Value')
->header('X-Header-Two', 'Header Value');
- 附加Cookies
return response($content)->header('Content-Type', $type)
->withCookie('name', 'value');
- Cookie的可定义属性
->withCookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly)
- 视图响应
return response()->view('hello', $data)->header('Content-Type', $type);
- JSON响应
return response()->json(['name' => 'Abigail', 'state' => 'CA']);
- JSONP 响应
return response()->json(['name' => 'Abigail', 'state' => 'CA'])
->setCallback($request->input('callback'));
- 文件下载
return response()->download($pathToFile);
return response()->download($pathToFile, $name, $headers);
- 重定向
return redirect('home/dashboard');
// 重定向到某个位置
return back()->withInput();
// 重定向至命名路由
return redirect()->route('login');
// 重定向至路由,路由带有参数
return redirect()->route('profile', [1]);
return redirect()->route('profile', [$user]);
// 重定向至控制器行为
return redirect()->action('HomeController@index');
return redirect()->action('UserController@profile', [1]);
4.注册中间件(Middleware)
中间件的注册,在laravel和Lumen中的方式是不一样的。
- 在Laravel中,通过app/Http/Kernal.php进行注册
protected $middleware = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
'Illuminate\Cookie\Middleware\EncryptCookies',
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession',
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
];
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
- 而在Lumen中,则是通过bootstrap/app.php注册
$app->middleware([
App\Http\Middleware\ExampleMiddleware::class
]);
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'jwt.auth' => Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => Tymon\JWTAuth\Middleware\RefreshToken::class,
]);
5. 服务提供者和服务容器
应用程序通过 bootstrap/app.php 文件中所生成的 Laravel 应用程序实例。 通过调用以下代码,创建应用程序Application的实例app,app中创建了服务容器(loadedProviders).
-- in bootstrap/app.php
$app = new Laravel\Lumen\Application()
-- in Application.php
protected $serviceProviders = array();
protected $loadedProviders = array();
protected $deferredServices = array();
...
$this->registerBaseBindings();
$this->registerBaseServiceProviders();
$this->registerCoreContainerAliases();
...
6.服务的注册(ServiceProvider)
ServiceProvider的注册,在Laravel和Lumen中情况不一样。
- Laravel
Laravel中的服务提供是,都在config/app.php配置文件的providers数组
'providers' => [
/*
* Laravel Framework Service Providers...
*/
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Bus\BusServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
'Illuminate\Routing\ControllerServiceProvider',
'App\Providers\AppServiceProvider',
'App\Providers\BusServiceProvider',
'App\Providers\ConfigServiceProvider',
...
];
- Luemn
Lumen中直接使用register进行注册
$app->register(Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class);
// Dingo API Register
$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);