Home > Net >  how to generate short link in laravel framework and relation with 3 model
how to generate short link in laravel framework and relation with 3 model

Time:04-30

I have created a short link system for my site, now how can I link it to my three models my Models

Article
Post
Music

i use this toturial for short link => https://laracasts.com/discuss/channels/laravel/generate-short-link#:~:text=some idea from-,here,-Reply

CodePudding user response:

That's easily done through routing. So in your route file it will be something like this...

Route::get('music/{slug}',  ['as' => 'music', 'uses' => 'music\MusicController@music']);

Then anything that comes in to your domain as https://yourdomain.com/music/slug will go to that music controller and you can process what slug is, then grab the model stuff you need and shoot them to a view.

If you really want nothing but short urls meaning https://yourdomain.com/shorturl you'll have to put the wild card at the root. This could have some undesired effects though if you don't have your routes set up in order. I'd probably put some middleware on the group too to process the shortUrl code and then send it on through to the controller where you can handle bringing back the model and shooting them to a view.

  • Related