3

I have the following test function, that I want to call directly from my URL or by clicking a link from my blade view.

public function callMeDirectlyFromUrl()
{
    return "I have been called from URL :)";
}

My Question: Is it possible to call a function directly from button-click or URL link from blade view in Laravel?

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137

1 Answers1

15

Here is the solution:

We assume you have a function callMeDirectlyFromUrl in YourController, here how you can call the function directly from URL, Hyperlink or Button.

Create Route

Route::get('/pagelink', 'YourController@callMeDirectlyFromUrl');

Add link in the view blade php file

<a href="{{action('YourController@callMeDirectlyFromUrl')}}">Link name/Embedded Button</a>

This has been tested on Laravel 4.2 -> 5.2 so far.

By clicking on this link or call the URL www.somedomain.com/pagelink the function will executed directly.

Community
  • 1
  • 1
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • How would get get back to the original page, after being directed to the "/pagelink" ? – Toolbox Aug 21 '20 at 05:02
  • I have not maintained my laravel skills for while, doing more .net. But what out digging deep in your question this can be done 2 or more ways, one simple way in javascript (window.history.back();) or in php/laravel, in your controller you can get current url some thing like "$request->path()", and cache is as key value, than you can fetch the key value and use it as return url. – Maytham Fahmi Aug 21 '20 at 06:15