LARAVEL 5.1

SESSION 03

Muhammad Rizwan Arshad

Principal Software Engineer, Nextbridge.

July 29, 2015

HTTP Requests

Accessing The Request

class UserController extends Controller
{

    public function store(Request $request)
    {
        $name = $request->input('name');

    }
}

 

 

Accessing The Request

Route::put('user/{id}', 'UserController@update');

 

class UserController extends Controller
{

    public function update(Request $request, $id)
    {
        //
    }
}

Retrieving The Request URI

$uri = $request->path();

 

if ($request->is('admin/*')) {
    //
}

 

$url = $request->url();

Retrieving The Request Method

$method = $request->method();

if ($request->isMethod('post')) {
    //
}

Retrieving An Input Value

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');

 

Determining If An Input Value Is Present

if ($request->has('name')) {
    //
}

Retrieving All Input Data

$input = $request->all();

Retrieving A Portion Of The Input Data

$input = $request->only('username', 'password');

$input = $request->except('credit_card');

Flashing Input To The Session

$request->flash();

 

$request->flashOnly('username', 'email');

$request->flashExcept('password');

Flash Input Into Session Then Redirect

return redirect('form')->withInput();

return redirect('form')
       ->withInput($request->except('password'));

Retrieving Old Data

$username = $request->old('username');

 

{{ old('username') }}

Retrieving Cookies From The Request

$value = $request->cookie('name');

Attaching A New Cookie To A Response

$response = new Illuminate\Http\Response('Hello World');
$response->withCookie(cookie('name', 'value', $minutes));
return $response;

 

$response->withCookie(cookie()->forever('name', 'value'));

Retrieving Uploaded Files

$file = $request->file('photo');

Verifying File Presence

if ($request->hasFile('photo')) {
    //
}

Validating Successful Uploads

if ($request->file('photo')->isValid())
{
    //
}

Moving Uploaded File

$request->file('photo')->move($destinationPath);

$request->file('photo')->move($destinationPath, $fileName);

HTTP Responses

Basic Responses

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);
});

Attaching Headers To Responses

return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');

Attaching Cookies To Responses

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)

Attaching Cookies To Responses

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',
];

View Responses

return response()
   ->view('hello', $data)->header('Content-Type', $type);

JSON Responses

return response()
   ->json(['name' => 'Abigail', 'state' => 'CA']);

 

return response()->json(['name' => 'Abigail', 'state' => 'CA'])
                 ->setCallback($request->input('callback'));

File Downloads

return response()->download($pathToFile);

return response()
   ->download($pathToFile, $name, $headers);

Redirects

Route::get('dashboard', function () {
    return redirect('home/dashboard');
});

 

Route::post('user/profile', function () {
    // Validate the request...

    return back()->withInput();
});

Redirecting To Named Routes

return redirect()->route('login');

 

// For a route with the following URI: profile/{id}

return redirect()->route('profile', [1]);

 

return redirect()->route('profile', [$user]);

Redirecting To Controller Actions

return redirect()
   ->action('HomeController@index');

 

return redirect()
   ->action('UserController@profile', [1]);

Redirecting With Flashed Session Data

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
Made with Slides.com