Run function from Button or URL in Laravel - laravel

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?

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
Link name/Embedded Button
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.

Related

View Composer for sidebar in Laravel

using a view composer for the first time in Laravel. i have a sidebar that is included in every step of a submission form process a user goes through. i want the sidebar to have link that apply to the proper submission (i.e. if this is submission number 5, the links in the sidebar should all go to the edit function for submission 5.
i have the following code in my web.php:
View::composer('layouts.planbuilder', function($view){
$plansubmissions = PlanSubmission::find(3);
$view->with('plansubmissions', $plansubmissions) ;
}) ;
i am able to access the $plansubmissions variable, but of course this only applies to submission 3, which i hard coded in. is it possible to get the logic from another controller? i can't just get the user id with Auth because a user can have many submissions
View Composers also have access to the variables that were passed to the view itself, so if you are passing the submission to your view from the controller like so
return view('submissions.show', compact('submission');
Then in your composer you can assign it to $plansubmissions
$plansubmissions = $view->getData()['submission'];

codeigniter - Do I need a Controller for every URL?

I have a working project on Codeigniter 3. Now I have to build a FAQ page and I had this doubt: do I need a Controller for every URL?
It is, the FAQ page is a static page, but CodeIgniter generally routes URLs to Controllers, like domain/controller/method. But it seems a waste to build a Controller to only load the View.
No, it's not right way to make controller for every page. Just make one function which shows page by fetching data from database.
First of all make a table named pages in your database then save page_content, page_name, permalink for your different pages.
Now suppose your default controller is home, make a function in it with name page as below.
function pages( $permalink )
{
// get page data based on page_name passed in URL
$this->db->where( array( 'permalink' => $permalink ) );
$data['page'] = $this->db->get( 'pages' )->result();
// load view and pass page object to view
$this->load->view( 'view_file', $data );
}
Now same function will show different page content based on permalink passed in URL.
For example if URL is www.example.com/index.php/home/pages/faq then content of faq page will be shown.

Laravel include view

I want to include a view like so: #include(user.myview), but within this view I need UserController logic. So I thought about calling a route: #include( route('user.route') ) which calls a Controllerfunction and returns the view but that isn't working. Any Ideas how to deal with this problem?
You need to create view composer and use it to get the data.
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
Simply add a link in you view and include it in your desired location.
Link will have a route.
On clicking the link, controller method can be called. e.g. show_link.blade.php
In your show_link.blade.php view:
<a href= {{route('route-name')}} > Click here</a>.
This route will call a method via .
Route::get('/call/method', 'controller#your_method_name')->name('route-name');
In controller, method your_method_name that will look like this:
public function your_method_name()
{
return "show what you want to";
}

Directly access a put/update method on Laravel 5.1 without going into the edit view (MethodNotAllowedHttpException in RouteCollection.php error)

I wanted to disable employees from a button on my index.blade.php page. Currently, the options of disabling employees (setting the status column in the database to false) is either to have an edit.blade.php view and update the value there, which is pretty standard for any laravel app or to have a new view for example, changestatus.blade.php, with the proper routes offcourse and update the value there. I am using the second implementation and it's working perfectly.
What i wanted to implement is to have a button on the index page which will change the status of the employee without going to a edit.blade.php or changestatus.blade.php page.
What i have tried
I have created new routes and created a button to link to the changestatus function
Routes.php
Route::put('employees/{employee}/changestatus', 'EmployeesController#changestatus')->name('employees.changestatus');
Route::resource('employees', 'EmployeesController');
EmployeeController
public function changestatus($EmployeeID)
{
$employee = Employee::find($EmployeeID);
$employee->status = true;
$employee->Save();
}
On my view i created a button with the following link
{{ URL::route('employees.changestatus', $employee->EmployeeID) }}
When i click that link, i get the MethodNotAllowedHttpException in RouteCollection.php error.
I even tried to change the Route::put to Route::Patch, but it's the same thing.
Is it even possible to achieve what I'm trying to do? If so, how?
When you click on a hyperlink, the web browser submits a GET request. Your route has been defined as being a PUT so that's why you're getting an exception.
You could either change the route to a GET by defining it like this:
Route::get('employees/{employee}/changestatus', 'EmployeesController#changestatus')->name('employees.changestatus');
Which isn't very ReSTful since a GET request should really only be used for returning a resource rather than modifying it.
Or, you could modify the button so that it submits a form like this:
<form method="post" action="{{ route('employees.changestatus', $employee->EmployeeID) }}">
{{ method_field('PUT') }}
<button type="submit">Button Text</button>
</form>
Note that you can't simply set the form method to PUT since this method isn't generally supported by web browsers. Laravel supports method spoofing which you can read all about here:
http://laravel.com/docs/5.1/routing#form-method-spoofing

Call an Action from an MVC View

I'm trying to figure out how to call an action from an MVC view, not call a view URL.
I have an Action Method for signing off in the Account Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SignOff()
{
authenticationService.SignOut();
return View("SignOff");
}
And I'd like to call it from a cshtml page, like this:
<li>#Html.Action("SignOff","Account")</li>
The list is part of a drop down menu.
ActionLink doesn't work because I don't have an Account/SignOff page. The above code returns the following: "Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'." ActionLink just tells me the resource doesn't exist.
Is there anyway to do this? I just want users to be able to select a "Logout" item from a menu and then have the application call the SignOff method. Do I need to use Ajax? Javascript? I'm still experimenting. Thank yoU!
EDIT: Ok, it was the http.post that was keeping me from calling this directly. I got rid of the acceptverbs attribute and now I can call the method directly.
You can just redirect to the Login page or to any other public page (home page).
return RedirectToAction("Login");

Resources