Showing posts with label Laravel Framework. Show all posts
Showing posts with label Laravel Framework. Show all posts

Saturday, February 25, 2017

A simple guide to using Traits in Laravel 5

Its great if we can use trait in our application. Laravel has the ability to give us that freedom to use trait. Laravel is a strong php based framework today. At first we have to create a trait, but its nothing but a php class except we use here a keyword named 'trait' & surely this class has no constructor as it cannot be instantiated. So lets start with use of trait.

First we have to create a php file named "TestTrait.php" as follows:

namespace App\Trait;

trait TestTrait {
    public function someFunction() {
        
    }
}

I think you have already have good knowledge about namespace described in Laravel.

Now we have to use this trait in some Controller or Service etc...

Lets see an example of use of trait in Controller.

First we have to import "TestTrait" in our Controller as follows:

//First step is import
use App\Trait\TestTrait;
class TalentMapperController extends Controller
{
    //Second step is tell our Controller to use trait
    use TestTrait;
    
    public function __construct() {
        //Third & final step is apply functions from here
        super::someFunction();
    }
}

Friday, February 24, 2017

Http Redirect Using Laravel

Laravel is a strong php based framework today. It is very important forward to another url or redirect as http function. There are several ways describes in Laravel. The best way is redirect using Controller & Action. Using Controller Action is easy to redirect to another url:

return redirect()->action(
    'SomeController@action', ['id' => 1, 'status' => 'active']
);


Or you also can do the following:

return redirect("/afterRedirect/param1?x=1&y=2");

Get Current URL Using Laravel

Laravel is a strong php based framework today. We frequently coding on this platform. And who coded its important for him to know / get current working url sometimes. Its easy to get current url inside an Controller/Service class. Just need to use Request attribute to get desired output at Laravel. 

First need to import Request class to Controller/Service class:
use Illuminate\Http\Request;

And then need to pass Request as parameter as follows:
public function someAction(Request $request)

And finally use following method to get current full url:
$request->fullUrl();

You have different options when you use Laravel Request class as follows:

Get the request method
$request->method();

Get the root URL for the application
$request->root();

Get the URL (no query string) for the request
$request->url();

Get the full URL for the request
$request->fullUrl();

Get the current path info for the request
$request->path();

Determine if the request is the result of an AJAX call
$request->ajax(); 


There is another way around to get URL from Laravel Controller
At first you have to import following component:
use Illuminate\Routing\UrlGenerator;

Then need to initialize it on construct on controller as follows:
private $UrlGenerator;
public function __construct(UrlGenerator $UrlGenerator)
{
    $this->UrlGenerator = $UrlGenerator;
}

And then use below line of code get Laravel base URL:
$urlGenerator->to("/");

You can get all available method details of UrlGenerator from below link:
https://laravel.com/api/5.0/Illuminate/Routing/UrlGenerator.html