I've got two buttons on my page, which each have different behaviours when clicked. Currently I've got my routes and controllers set up like this...
routes/web.php
Route::post('/users/button1clicked', 'UsersController#button1clicked');
Route::post('/users/button2clicked', 'UsersController#button2clicked');
app/http/controllers/UsersController.php
class UsersController extends Controller
{
public function button1clicked(Request $request){
//Do something
}
public function button2clicked(Request $request){
//Do something else
}
}
It works ... but I don't think I'm following the correct convention for my controller, because controllers should just have the standard actions (index, create, store, show, edit, update, destroy).
What would be a better way for me to structure this code?
What does each button do? For example, if the button submits a form to 'save' an entity (say a 'Post' in a Blog app) then the form action can direct to the PostController#store. If the button click is intended to show a html form to create an Post, then the it can lead to PostController#show. The table below from the Laravel docs will help you.
Route definitions conventions
Please also see https://laravel.com/docs/7.x/controllers#restful-naming-resource-route-parameters.
If you are using ajax or axios to make async calls then you can call a function using button events (on-click for example) and that function can make a post (or any other async call to the relevant controller method ( PostController#store). Please let me know if you'd like an example.
Related
I have a Data model that I want people to be able to view individual records of and then edit/add data to. I have managed to get the view route working;
Route::get('/data/{data_token}', 'DataController#show');
Data_token, is a unique string. This then uses this DataController function;
Public function show($data) {
$data = Data::where('data_token',$data)->first();
return view('data.show', compact('data'))
}
After which I can display the data on the page, and have a form for editing (actually its for adding data that doesn't exist, but whatever, same principle right).
On the form on the data.show view, I am sending it to a different view;
Route::get('/data/{data_token}/edit', 'DataController#edit');
This can use the $request variable to return the forms values, but I can't relate it to the data row I was previously editing?
how do I get the {data_token} passed to the edit function of the controller?
Edit( adding route files)
Noticed I forgot the {'data_token'} in the post route.
/Begs forgiveness
I think you've misunderstood how the routes and controllers work. What you're looking at is a fairly simple CRUD setup like the following;
Route::get('/data/{data_token}', 'DataController#show');
Route::get('/data/{data_token}/edit', 'DataController#edit');
Route::post('/data/{data_token}/edit', 'DataController#update');
Now your controller would have;
public function show($dataToken) { ... }
public function edit($dataToken) { ... }
public function update($dataToken, Request $request) { ... }
Then you'd have your form on the edit view like so;
<form action="{{ route('DataController#update') }}" method="post">
Laravels router will always try to pass in the URI variables as arguments to the methods provided.
Providing that I have understood what you need, this should suffice.
For example I have edit profile page which have a form for editing the summary. the file's name is Index.tpl.
In form I have a text field, I have added the saveSummary() in the controller i.e. controller.php. How can I invoke the given function on clicking on submit button of form.
Answering directly to your question, you should submit the form to /save-summary/ action if it's called saveSummary() in your controller. Of course don't forget to include prefix of the route.
Generally your approach is not correct because you're trying to use different actions for displaying content and processing the form – you can easily do both operations in one action. Check for isPost() and getPost() in other controllers – this methods are used to divide parts of action responsible for simply getting and displaying content and for processing form data.
You can access controller functions by using action handle at the end of function name ex -
class Mymodule_MytestController extends Core_Controller_Action_Standard{
public function saveSummaryAction(){
.......
}
}
I have 5 controllers and 5 models and they are all related to backend. I can easily output data in the backend but I need to that for the frontend as well. Not all of course but some of them.
For example I have controller called BooksController:
public function getBooks(Request $request)
{
$books = Books::all();
return view('backend.books.show', compact('images'));
}
So this will show it in backend without any problems but what I want is for example to loop through all the books and show their images in welcome.blade.php which doesn't have controller.
And also to pass other parameters to that same view from different controllers.
Is this is possible?
Thank you.
You are having an error because you did not declare the variable $image
public function getBooks(Request $request)
{
$books = Books::all();
$images = array_map(function($book) {
$book->image;
}, $books);
return view('backend.books.show', compact('images'));
}
It sounds like you are potentially caught up on some terminology. In this case, it sounds like backend is referring to your admin-facing interface, and frontend is referring to your user-facing interface.
You also seem to be locked on the idea of controllers. Unless the route is verrrrrry basic, create a controller for it.
Have a controller for your welcome view, for your admin view, basically (with some exceptions) a controller per resource or view is fine.
In this case, you would have one controller for your admin book view, and a seperate controller for your welcome view. Both of which would pull the books out of the db and render them in their own way
Route::get('admin/user/add', function()
{
if (!Input::has('submit'))
return View::make('theme-admin.user_add');
else
Redirect::to('admin#user_add_process');
});
Normal GET request to admin/user/add shows the register form. However, when the form is submitted, I have to redirect it to action_user_add_process() function of Admin controller so I can save values to database.
The solution above doesn't work and I get 404's.
I call form action like this:
action="{{ URL::to('admin/user/add') }}">
How can I solve this issue?
Ps. If there is a shorter way to achieve this, let me know!
You need to specify that you are redirecting to a controller action using the to_action() method of Redirect.
return Redirect::to_action('admin#user_add_process');
Alternatively, you could just use this URL that doesnt even use the route you created making the if/else irrelevant.
{{ URL::to_action('admin#user_add_process') }}
On a third note, keeping your routes clean makes maintainence alot easier moving forward. As routes use a restful approach, take advantage of it. Using the same URL you can create and return the View with a GET request and submit forms with a POST request.
Route::get('admin/user/add', function() { ... }
Route::post('admin/user/add', function() { ... }
You can also have your routes automatically use a controller action like this:
Route::post('admin/user/add', 'admin#user_add_process');
I'm developing a web application with Zend Framework 1.12, which is something new to me, and I'm not sure about the way to do something I want to.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
On my home page, the module Index, I made what I wanted to do with it, created several actions and all the stuff, but I'd like to add a search engine I'll make myself.
The problem is that I'd like to create the search engine as a separate module named Search, for example, but put the SearchForm in the home page. Hitting submit would send the datas from the form to the Search module.
I don't quite understand how to do that without having to go to /search to access my form and every associated actions.
Do I have to use a View Helper ?
Also, the searchForm in the front page would be some sort of QuicKSearch and accessing /search would show a more elaborated form for the research.
Can someone explain me how to access the searchForm from the Index module or redirect me to the part of the documentation talking about that ? My research are unsuccessful and Google doesn't help me either.
EDIT: When I talk about Module, I mean Controller, sorry for that, I still mistake the terms ...
First of all, build the searchform as viewHelper, then you can reuse it in several views.
The action attribute in form snippet set to searchModule/controller/action.
Additionaly make research about viewHelpers and Forms in Zend Documentation.
I actually prefer to do this as a an action helper and then just use a standard placeholder view helper to present the search form.
let me demonstrate:
the actual action helper just initiates a form and prepares it for display. I'll leave the form structure to you.
//the action helper
//Just fill in the args for the form to be displayed
class NameSpace_Controller_Action_Helper_Search extends Zend_Controller_Action_Helper_Abstract
{
public function direct($action, $label = null, $placeHolder = null)
{
$form = new Application_Form_Search();
//set the action
$form->setAction($action);
//set the submit button text
$form->search->setLabel($label);
//set the hint text displayed in the form window
$form->query->setAttribs(array('placeholder' => $placeHolder,
'size' => 27,
));
return $form;
}
}
I put the helper in the predispatch method of the controller so that each action in the controller can use the search form with having to build it in every page.
//to use the helper in your controller
class IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
//setup action helper and assign it to a placeholder
$this->_helper->layout()->search = $this->_helper->search(
'/index/display', 'Search Collection!', 'Title');
}
//in your view script
<?php echo $this->layout()->search ?>
I like to put the placeholder in my master layout.phtml so that any time I populate the placeholder it will display. Now all you have to do is style it however you want.
Remember: As with any html form the action parameter is just a url so any valid url can be assigned to the form action. In this example I used the /controller/action parameters, but there are many other ways to pass a url to the form. The url helper comes to mind as good way to do it.
url($urlOptions, $name, $reset, $encode): Creates a URL string based
on a named route. $urlOptions should be an associative array of
key/value pairs used by the particular route.