SESSION 03
Muhammad Rizwan Arshad
Principal Software Engineer, Nextbridge.
July 29, 2015
class UserController extends Controller
{
public function store(Request $request)
{
$name = $request->input('name');
}
}
Route::put('user/{id}', 'UserController@update');
class UserController extends Controller
{
public function update(Request $request, $id)
{
//
}
}
$uri = $request->path();
if ($request->is('admin/*')) { // }
$url = $request->url();
$method = $request->method(); if ($request->isMethod('post')) { // }
Not need to worry about the HTTP verb: $name = $request->input('name');
Pass a default value:
$name = $request->input('name', 'Sally');
When working on forms with array inputs:
$input = $request->input('products.0.name');
if ($request->has('name')) { // }
$input = $request->all();
$input = $request->only('username', 'password');
$input = $request->except('credit_card');
$request->flash();
$request->flashOnly('username', 'email'); $request->flashExcept('password');
return redirect('form')->withInput();
return redirect('form')
->withInput($request->except('password'));
$username = $request->old('username');
{{ old('username') }}
$value = $request->cookie('name');
$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
return $response;
$response->withCookie(cookie()->forever('name', 'value'));
$file = $request->file('photo');
if ($request->hasFile('photo')) { // }
if ($request->file('photo')->isValid()) { // }
$request->file('photo')->move($destinationPath);
$request->file('photo')->move($destinationPath, $fileName);
Route::get('/', function () {
return 'Hello World';
});
Route::get('home', function () {
return (new Response($content, $status))
->header('Content-Type', $value);
});
Route::get('home', function () {
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');
return response($content)->header('Content-Type', $type)
->withCookie('name', 'value');
The withCookie method accepts additional optional arguments:
->withCookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly)
All cookies generated by Laravel are encrypted and signed.
To disable encryption, use the $except property of the App\Http\Middleware\EncryptCookies middleware:
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'cookie_name',
];
return response()
->view('hello', $data)->header('Content-Type', $type);
return response()
->json(['name' => 'Abigail', 'state' => 'CA']);
return response()->json(['name' => 'Abigail', 'state' => 'CA'])
->setCallback($request->input('callback'));
return response()->download($pathToFile); return response() ->download($pathToFile, $name, $headers);
Route::get('dashboard', function () { return redirect('home/dashboard'); });
Route::post('user/profile', function () { // Validate the request... return back()->withInput(); });
return redirect()->route('login');
// For a route with the following URI: profile/{id}
return redirect()->route('profile', [1]);
return redirect()->route('profile', [$user]);
return redirect()
->action('HomeController@index');
return redirect()
->action('UserController@profile', [1]);
Route::post('user/profile', function () {
// Update the user's profile...
return redirect('dashboard')
->with('status', 'Profile updated!');
});
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif