Laravel, call Action from a Controller from a View?

3

I have a View where in it I would like to add the link of a Controller with its Action specifies and when loading this View to Action of that Controller is triggered.

Example: within a View I have a call of controller Home@index that when this action is called will render View of that Action .

    
asked by anonymous 27.06.2017 / 07:01

2 answers

4

There's nothing ready for what I could check out, but there are ways to do it:

Minimum example

Controller

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SortController extends Controller
{
    public function index()
    {
        return view('sort');
    }

    public function trecho(Request $request)
    {
        $url = $request->fullUrl();
        return view('trecho', ['url' => $url]);
    }
}

Routes.php

Route::get('/sort', ['as' => 'sort.index', 'uses' => 'SortController@index']);
Route::get('/sort/trecho', ['as' => 'sort.trecho', 'uses' => 'SortController@trecho']);

View sort

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="robots" content="noindex, nofollow">
    <meta name="googlebot" content="noindex, nofollow">        
    <title>View Sort</title>
</head>
<body>
    <h1>Sort</h1>
    {!! app(App\Http\Controllers\SortController::class)->trecho(app('request')) !!}
</body>
</html>

In controller there are two methods one that will start and the other that will be called by View sort.blade.php . In your View call the command:

{!! app(App\Http\Controllers\SortController::class)->trecho(app('request')) !!}

that execution will play its part in bringing the generated View .

You can also generate a blade extension as follows, open the file app\Providers\ServiceProvider.php and import the namespace : Illuminate\Support\Facades\Blade; and within the boot method add the following code to create a new blade for your project:

public function boot()
{   
    Blade::directive('action', function($expression) {
        $vars = explode(',', $expression);
        $ctrl_string = trim(array_shift($vars));
        $meth_string = trim(array_shift($vars));
        $ctrl = app($ctrl_string);
        $items = array_map(function ($var){
            return eval("return $var;");
        }, $vars);
        $view  = (is_array($items) && count($items) > 0)
                ? call_user_func_array(array($ctrl, $meth_string), $items)
                : call_user_func(array($ctrl, $meth_string));
        return "<?php echo '".$view."'; ?>";
    });
}

In View call as follows:

@action(App\Http\Controllers\SortController,trecho,app('request'))

27.06.2017 / 23:05
0

One way I constantly use my tests is through Laravel's own Helpers

.

I'll use this route in the routes \ web.php file:

Route::get('/diretorio','Seucontroller@action')->name('rota.teste');

To call the controller during any view you could simply do the following in the view make the call:

<a href="{{route('rota.teste')}}"> Link de teste </a>

Using laravel 5.5 helpers

    
26.10.2017 / 03:44