Saturday, May 28, 2016

Laravel 5 : How to check current URL or Route

Hi

Today we will learn how to check or get current url or route.

Sometimes we need to check current URL or route and do some action.

1. Check if URL = myurl

Simply – we need to check if URL is exactly like myurl and then we show something.

In Controller:

if (\Request::is('myurl')) {
  // do some thing
}

In Blade file – almost identical:

@if (\Request::is('companies'))
  // do some thing
@endif


2. Check if URL contains myurl

A little more complicated example – method Request::is() allows a pattern parameter, like this:


if (\Request::is('myurl/*')) {
  // will match URL /myurl/999 or /myurl/create
}


3. Check route by its name

As we know, every route can be assigned to a name, in routes.php file it looks something like this:

Route::get('/myroute', ['as' => 'myrt', function () {
  return view('myroute');
}]);

So we can check if current route is myrt

if (\Route::current()->getName() == 'myrt') {
  // We are on a correct route!
}

So these are three ways to check current URL or route.

Thanks

Friday, May 20, 2016

Laravel 5 Validation rules "sometimes"

Hi

Today we will discuss validation rules "sometimes", apply rules only if field exists.

Imagine that we have two forms – one with username/password fields and another with email/password fields. And we want to have the same validation rules (either in Request, or directly in Controller, doesn’t matter).

And we need to validate if the email is filled in ONLY if that field is present within the form. Then we call it ‘sometimes’.

$this->validate($request, [
  'email' => 'sometimes|required|email'
]);

To understand better, Let’s look at the difference between this code and the “simple” one:

$this->validate($request, [
  'email' => 'required|email'
]);

The second validation will fail if the email field is empty or is not in the form at all. That’s the key point – the first validation will fail only when the field is present but empty.

It is simple but I hope that it will help someone.

Thanks

Friday, May 6, 2016

How to Log every request & response in Laravel 5

Hello Everyone,

Today we will discuss about how to log every request & response in Laravel 5.1.

Sometimes it's useful to log some/all requests to our application. This is really convenient when we use Laravel to build our APIs.

A logging middleware might log all incoming requests to our application. In Laravel 5.1 there is a terminate method and it's call after the sending HTTP response to the browser. This way we have access to both $request and $response at the same time.

Let's take a look at how we are going to achieve this in Laravel 5.1:

Create a new middleware by typing this command in terminal inside our project directory.

php artisan make:middleware LogAfterRequest

And then put below code in it(project_dir/app/Http/Middleware/LogAfterRequest.php).

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogAfterRequest {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
    }

}

In this code terminate method receives $request and $response. These are objects which will give us all of the handy stuffs we probably need to log.

When we have our middleware ready, we should add it to our HTTP Kernel.

Open Kernel.php and add this line to our protected $middleware property:

\App\Http\Middleware\LogAfterRequest::class

That's it. We can additionally filter what we want to actually log, but this is the basics.

Thanks.