-2

I want to remove one part of the URL to make the url shorter in the laravel application.

This is my routes definition:

Route::get('/category/{slug}','CourseController@courseCategory');
Route::get('/course/{slug}','CourseController@detail');

Example I have customer service and sleep care categories. And a course named Sleep care lvl 1. So I can access to the categories and courses by these links:

  • mydomain.com/category/customer-service
  • mydomain.com/category/sleep-care
  • mydomain.com/course/sleep-care-lvl-1

May I know how can I make the URL shorter by removing category and course in the URL then the urls will become:

  • mydomain.com/customer-service
  • mydomain.com/sleep-care
  • mydomain.com/sleep-care-lvl-1

Thank you very much!

  • 1
    Update your route and create a controller method that can distinguish between Courses and Categories – brombeer Mar 20 '20 at 08:54
  • Hi @kerbholz. Thank you for answering. It's really an idea! But do you think is there any way to do without update the route?. – Vinh Hoang Mar 20 '20 at 08:59
  • Create a new route? – brombeer Mar 20 '20 at 09:02
  • do you think we can handle by htaccess? – Vinh Hoang Mar 20 '20 at 09:04
  • 1
    There would be a way to do this, you could create a single controller that will take care of your Routing and do something like a switch statement to handle the actions it should take, but in all honesty that is not a good approach. The routes and names should follow a consistent convention –  Mar 20 '20 at 09:04
  • Not sure what you're after and why you don't want to update/create your routes or why you want to do it via .htaccess. Routes exist for a reason. I might be possible to do via .htaccess but that sounds like the harded/more complicated way to do it. – brombeer Mar 20 '20 at 09:06
  • 1
    @kerbholz. it's same as Devin Gray said. we have to handle to switch between course and category. and there are also other routes have single name in my application like contact and about page. they are also mydomain/contact , mydomain/about – Vinh Hoang Mar 20 '20 at 09:12
  • 1
    Seconded, You should not change it, the way it is now is already perfect –  Mar 20 '20 at 09:14
  • 1
    /contact and /about should not interfere with your category/course routes. Define them first and you're good to go. You will get problems though if you have a category or course named "contact". – brombeer Mar 20 '20 at 09:17

1 Answers1

0

In the end, the routes are just strings.

The easiest way to achieve what you want to do is by updating the routes

Route::get('/category/{slug}','CourseController@courseCategory');
Route::get('/course/{slug}','CourseController@detail');

Would become

Route::get('my-short-route','CourseController@courseCategory');
Route::get('my-other-short-route','CourseController@detail');

However this does not solve your question and is not a good solution,

but does act as a point of interest.

What you are trying to do is impossible without refactoring the entire application. There is no .htaccess that can help this because you need to have an entry point into the application.

That entry point cannot be a wilcard variable.

Meaning you won't be able to do this

Route::get('{slug}','CourseController@courseCategory');

Because that will act as a catch all

You could do something like

  • mydomain.com/this-is-a-random-string

Which will fire the courseCategory method on the CourseController

The way it is the correct convention, I would not change it