Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nklvjvc's avatar

Php for beginners 2023 - pretty urls

Hi, I have been watching Php for beginners 2023. On episode 22, Jeffrey said he would show us how to make pretty urls but he forgot about it. $router->get('/note', 'controllers/note.php'); Is there a easy way to show ID and SLUG into url from this router

0 likes
3 replies
jlrdw's avatar

Basically you pass parameters instead of using a query string.

1 like
nklvjvc's avatar

@jlrdw Hi, appreciating trying to help. Could I ask you to elaborate a bit. Whatever I have tried has failed. To be honest I don't quite understand Router class that Jeffrey made. Would you be willing to have a quick look at it if I post part of code here.

christogonus's avatar

I assume that you have a slug field or related field that you want the url to carry. I will assume it is "slug". I assume that the model is "Note" and "notes" is the database table name.

Your code doesn't follow laravel conventions ~ so well. I know you are free to do what you want to do as a seasoned programmer, but I will try to show how I'd achieve that following laravel convention.

In your routes/web.php, instead of

$router->get('/note', 'controllers/note.php');

I will name my controller "NoteController.php". And my code will be as below the following:

Route::get('note/{note:slug}', [App\Http\Controllers\NoteController, 'show']);

in the above, "show" is the method in controller and my show method will be like:

use App\Models\Note;

public function show(Note $note) {
		return $note;
}

Please or to participate in this conversation.