I'm a beginner to Laravel, and also to front-end oriented programming (most of my background is in database management). I'm attempting to put together a multiple page form that allows users to move through a hierarchy of data:
- on the first page, they can select a project or create a new one,
- on the second they select a subsection of that project (or again, make a new one)
- on the third page they can perform file uploads or specific tasks for that
subsection.
My hacked-together way of doing this was simply to have each page after the first be a POST route that grabs the identifying data of the previous step to generate the correct select fields for the view and populate the database properly. However, after some research I've learned that directly calling views from a post route is a clumsy way of doing things (mixing up GET and POST methodology), made especially evident by the fact that validation error redirects cannot work with post routes. Thus, if the user ever messes up in the form, rather than being redirected to the last stage with a list of errors, they end up with a Method Not Allowed exception.
What is a better (RESTful) way of achieving this kind of multiple-page form? I'm not very good at front end stuff and would rather avoid Ajax if I can.
Here's some of my code as it stands:
route file:
Route::get('newentry', 'NewEntryController@index');
Route::get('newentry/selectjob','NewEntryController@selectjob');
Route::post('newentry/selectday','NewEntryController@selectday');
Route::post('newentry/upload','NewEntryController@upload');
example method for a form page
public function selectday()
{
//get post from job form and validate
$request = Request::all();
$job_id = intval(Request::get('job_id'));
//create data variable, pass job_id for hidden field
$data = [];
$data['job_id'] = $job_id;
//apply ruleset
$rules = array(
'job_id' => 'required'
);
$validator = Validator::make($request, $rules);
if ($validator->passes()) {
//get the subset of days
$data['days'] = DB::table('daily_runs')
->where('job_id',$job_id)
->orderBy('id', 'asc')
->lists('start_time','id');
return View::make('newentry.day')->with('data', $data);
} else {
return Redirect::to('newentry/selectjob')->withInput()->withErrors($validator); //this redirect will not work.
}
} //end selectday
This code will work if the user makes no mistakes (as I mentioned, if they mess up the validation redirect will throw a Method Not Allowed exception) but from what I'm hearing I'm approaching it with poor coding practice. What steps could I take in the controller/view structure to make it better?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…