How to generate dynamic urls

Asked

Viewed 17 times

-1

I’m working on a project for writers to publish poems and poetry. Urls will be separated by gender, for example: site.com/poems/field flowers/

However, I need to avoid double titles. Because of this I thought I’d do a scheme similar to Wattpad that creates the url like this: Wattpad.com/127883554-the-new-Avenger

They enter the code "127883554" before the title. But I have some questions:

1 - As Gero this code representing day, month and year without being 26052019 ?

2 - How will I mount the url so that code+title is separated by "-".

The Laravel has some feature to mount these urls in an intelligent way?

1 answer

0

You can do this when registering example:

public function cadastrar(Request $request) {
       $request->validate([
            $request->titulo => 'required',
            ...
       ]);

       Model::create([
          'table_titulo' => $request->titulo,
          'table_url' => mt_rand(0, 9999999).'-'.mt_rand(0,10).$request->titulo,
          ...
       ]);

}

This way you register the URL and title, when accessing something example Wattpad, only do a route this way;

Route::get('/{url}', 'livroController@index');

controller will stay that way

public function index(Request $request) {
     $livro = Livros::where('url', $request->only('url'))->first();

     return view('index', compact('livro'));

}

Make a test if it works of an Alo ai...

NOTE: If it doesn’t work with Request trade by $url the parameter to be received...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.