Laravel, call Action from a Controller from a View?

Asked

Viewed 7,000 times

3

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

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

  • 1

    Your doubt is causing new ones! Are you willing to work with ajax? or are you willing to carry a View inside each other by Action? if it could edit and try to explain in more detail, it was not very clear

  • 1

    I’m looking to load one view into the other by the action

  • Post this View? with this call! Why so with @include I would, so I need to know what you really want.

  • It’s close to that, but I don’t think the technology exists in the basement yet. When I was programming in ASP.NET I had a Helpers in Razor which is the ASP.NET Engine Template that generated a partial view for me, where he called it from a controller action. =/

  • 1

    I understand I don’t think there is but I’ll take a look.

  • 1

    Thanks so much for your cooperation!

  • Place a new edition with a blade created... much like what you said on Asp.net

Show 2 more comments

2 answers

4


There’s nothing ready for what I can verify, but there are ways to execute:

Minimal 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 the controller there are two methods one that will start and the other that will be called by View sort.blade.php. In his View call the command:

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

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


Can also be generated a blade extension As follows, open the file app\Providers\ServiceProvider.php and import the namespace: Illuminate\Support\Facades\Blade; and within the method boot 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 the View call as follows:

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

References:

0

A way that I constantly use in my tests are through Helpers of Laravel himself.

I will use as an example this route in the file Routes web.php:

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

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

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

Use of helpers from Standard 5.5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.