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

srikanthgopi's avatar

Category in urls

i'm creating an application where i have ArticleCategories and Articles tables.

And i have created Articles index page where i can list all the articles according to their categories. For example Category 1 -article1 -article2 -article3 view all

Category 2 -article1 -article2 -article3 view all

When i click on category1 i want to go to a new page where i can list all the articles in category1 with pagination similarly for other categories.

When i click on category1 and article1 i wan to to the a page showing that article. And the category name should be automatically included in the URL.

So im trying to achieve these route /articles {which lists all the articles in all categories} /articles/category-name {which lists all the articles in that category} /articles/category-name/article-title {to view article in that category}

I'm learning laravel so what i did is created categories and a new route, method in controller and views accordingly. For example, Route::get('/articles/health', 'ArticleController@health')->name('health'); Route::get('/articles/health/{slug}', 'ArticleController@healthshow');

and i have created health and healthshow methods in Article Controller. And in views i have created a folder articles and subfolder health and in that i have created a index and show view files.

Similarly i need to create for each category which works fine. But the problem is i feel this is not dynamic as whenever i create a category i have to create a view folder which is same as category slug and a route. controller method accordingly.

Could someone explain me how to achieve this kind of functionality dynamically?

I also have profiles listed in my project so i want to achieve the following too /doctors/hyderabad/dermatologist/{slug}

Where /doctors route should show all the doctors in all categories and /doctors/hyderabad should show all the doctors in hyderabad and /doctors/hyderabad/dermatologist should show all doctors in hyderabad with dermatologist specialty. My tables are DoctorSpecialties, Doctors, Countries, States, Cities

I don't need code. I want to understand the concept how to achieve these kind of urls dynamically so that i can understand and code myself to learn the framework better. I guess i will need these kind of urls in every project

0 likes
2 replies
Dunsti's avatar
Dunsti
Best Answer
Level 6

I guess, you see this too complicated.

lets say, you have a route like this:

Route::get(
            '/{category}/{article}}',
            ['as' => 'article.detail', 'uses' => 'ArticleController@show']
        );

then your ArticleController receives the two fields {category} and {article} as parameters:

public function show(Category $category, Article $article)
{
    ...
}

You then have a complete Category-Model and a complete Article-Model available in your Controller-method. You could possibly do something like this: dump($article->name)

But: in your URL you would need to enter the IDs of the category and article you want. This works, but it doesn't look that good.

You can change the field that is used for the URL in your Model like this:

    /**
     * Defines the attribute that is used to route to the object
     */
    public function getRouteKeyName()
    {
        return 'name';
    }

this would use the name-field in your Database to fetch the correct Model you use in the controller.

Does that help?

1 like

Please or to participate in this conversation.