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

Tuesday, July 11, 2017

Laravel 5: Get Controller Name in View | Route Details | Controller & Action Name | Route Parameters

You can create a view composer that injects those variables into your view file. In "app/Providers/AppServiceProvider.php" add something like this:

public function boot()
{
    //app('view')->composer("folder.specific_view_name", function ($view) {
    app('view')->composer("*", function ($view) {
        $action = app("request")->route()->getAction();

        $controller = class_basename($action["controller"]);

        list($controller_name, $action_name) = explode("@", $controller);

        $view->with(compact("controller_name", "action_name"));
    });
}

You also can parameters from current request as below:

app('request')->route()->parameters()

Array
(
    [middleware] => web
    [as] => folder.view_name
    [uses] => App\Http\Controllers\UserController@show
    [controller] => App\Http\Controllers\UserController@show
    [namespace] => App\Http\Controllers
    [prefix] => 
    [where] => Array
        (
        )


)


Friday, June 16, 2017

Laravel 5.X: Access HTTP Request From Controller | Service

It's very important to access HTTP Request from Laravel Controller or Service. Below is a code example showing how to access HTTP Request from Laravel Controller. Same for Service.


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request as HttpRequest;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    private $HttpRequest;
    public function __construct(HttpRequest $HttpRequest)
    {
        $this->HttpRequest = $HttpRequest;
    }

    public function checkHttpRequest() {
        echo $this->HttpRequest->get("id") . "<BR>";
        echo $this->HttpRequest->getBaseUrl() . "<BR>";
        echo $this->HttpRequest->getUri();
        die();
    }
}









Friday, April 14, 2017

Laravel 5.x Install DOM-PDF

Laravel is a strong php based framework today. Its very important that we can create PDF using our application. Laravel & DOM-PDF both makes it easy. Just follow the following steps:

1. Add the following line to "composer.json" in "require" section:

"barryvdh/laravel-dompdf": "^0.8.0"

2. Open command prompt and navigate to project directory & execute following command:

composer update barryvdh/laravel-dompdf --lock

Above command will install dompdf in your project

3. After installation open "conig/app.php" and add following line to providers:

Barryvdh\DomPDF\ServiceProvider::class

4. Add the following line to create an alias for dompdf:

'PDF' => Barryvdh\DomPDF\Facade::class

5. Now create a folder named "fonts" in "storage" folder.

This folder will used to store font cache.

6. Now create a Controller as follows:



<?php
namespace App\Http\Controllers;

use PDF;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    public function pdf()
    {
        $pdf = PDF::loadView('home.pdf');
        //return $pdf->download('review.pdf'); //Download        
        return $pdf->stream('review.pdf'); //Stream    
    }
}
?>


7. Now create a view under "resource/views/home" named "pdf.blade.php" with following contents:


<!DOCTYPE html>
<html lang="en" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
</head>
<body>
<h1>Test PDF</h1>
</body>
</html>


8. Add the following line to "routes/web.php":


Route::get('/pdf', "HomeController@pdf");

9. Now browser {{Base_Url}}/pdf to view your pdf:




10. You are done.

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