0

May I know how can I make just a single route so I don't have to repeat it? Thanks in advance.

Route::get('/url', 'CtcFormController@index' )->name('CtcForm.ch');
Route::post('/url/submit', 'CtcFormController@submit')->name('CtcForm.submit');
Route::view('/url/submitted', 'form.submit')->name('CtcForm.submit');

Route::get('/url2','CtcFormController@store')->name('CtcForm.eng');
Route::post('/url2/submit', 'CtcFormController@submit')->name('CtcForm.submit');
Route::view('/url2/submitted', 'form.submit')->name('CtcForm.submit');
2
  • 1
    You can use resource route Commented May 20, 2021 at 4:23
  • your submit and submitted url are same..going to the same controller method. you can merge them. but just url is different. you can't merge them. Commented May 20, 2021 at 4:32

1 Answer 1

1

As per your given example, you want to handle the variable part of the route which is /url/ and /url12/. Yes! you can handle there both different route using a single route in ways: Use route variable to handle dynamic url values i.e. url, url2,url3...url12 and so on.

Route::get('/{url}', 'CtcFormController@index' )->name('CtcForm.ch');
Route::post('/{url}/submit', 'CtcFormController@submit')->name('CtcForm.submit');
Route::view('/{url}/submitted', 'form.submit')->name('CtcForm.submit');

Now in your controller methods handing above routes receive extra parameter $url like: In controller CtcFormController.php class:

public function index(Request $request, string $url) {
       //$url will gives you value from which url request is submitted i.e. url or url12
      //method logic goes here ...
} 

Similarly, method handling /{url}/submit route will be like:

public function submit(Request $request, string $url) {
    //method logic goes here ...
}

Let me know if you have any further query regarding this or you face any issue while implementing it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.