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

Ifrit's avatar

How to get a title and id

This might be a stupid question but how does one get the $title and $id for a function? In my url I have

localhost/site/{title}/{id}

but I would like to have it as

localhost/site/category1/item1

where {title} would be the category name and {id} would be the id of that item

I'm using Laravel 4.2

My route.php

Route::get('/{title}/{id}', array('as' => 'portfolio', 'uses' => 'HomeController@portfolio'));

My function

public function gallery($title, $id){
        echo "string";
    }
0 likes
16 replies
mstnorris's avatar

That'e exactly it however your Route defines a method of portfolio yet you've shown a method called gallery. Which is it?

HomeController@portfolio

Inside your function:

public function gallery($title, $id) {
    echo "ID: " . $id . " and the title is: " . $title;
}

where {title} would be the category name and {id} would be the id of that item

What relationship do you have between Category and Item?

Ifrit's avatar

Sorry I had meant to change gallery into portfolio. I have a many to many relationship between category and item. When I echo the function I get

ID: {id} and the title is: {title}

Which as far as I understand it means that it isn't picking up anything

mstnorris's avatar

Can you post your full routes.php file and your Controller here, along with what URL you're hitting in the browser.

Ifrit's avatar

@mstnorris - Sure.

routes.php

Route::get('/', 'HomeController@index');

Route::get('/{id}', 'HomeController@content');

Route::get('/{title}/{id}', array('as' => 'gallery', 'uses' => 'HomeController@gallery'));

HomeController.php

<?php

class HomeController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */

    public function index()
    {
        //Gets the menu child item where it belongs with its parent. //Need to rephrase that better
        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
        //Gets the home menu. It checks to see if the ID is 1 or if the title is home
        $menu = Menu::where('id', 1)->orWhere('title', 'home')->firstOrFail(); 
        //This gets the menu type which determins the template that is to be used
        $layout = $menu->type;

        $content = Content::where('id', 1)->orWhere('title', 'home')->firstOrFail();

        //This gets all the contact information
        $contact_bottom = Contact::all();
        //This gets all the social media information
        $social_media = SocialMedia::all();
        //This gets all the menus
        $main_menu = Menu::all();

        $portfolios = Portfolio::orderBy('created_at', 'desc')->limit(6)->get();

        return View::make('index', compact('menus_child', 'main_menu', 'content', 'contact_bottom', 'social_media', 'portfolios'))->with('menu', $menu, $main_menu, $content, $contact_bottom, $social_media, $portfolios);
    }

    public function content($id)
    {

        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();

        $menu = Menu::where('id', $id)->firstOrFail();

        $layout = $menu->type;
        return View::make('public/'.$layout, compact('menus_child'))->with('menu', $menu);
    }

    public function gallery($title, $id) {
        echo "ID: " . $id . " and the title is: " . $title;
    }
}
mstnorris's avatar

Try reordering your routes.

Route::get('/{title}/{id}', array('as' => 'gallery', 'uses' => 'HomeController@gallery'));
Route::get('/{id}', 'HomeController@content');
Ifrit's avatar

@mstnorris - I've reordered my routes and I still get

ID: {id} and the title is: {title}
mstnorris's avatar

Something else is going on as your routes.php file and your controller look fine to me.

Ifrit's avatar

@mstnorris - This is my blade where I'm calling the route.

<div class="row">
    <div class="col-lg-12 gallery">
        <ul class="pics">
            @foreach($pages->pcategory as $port)
                <?php
                    $portImage = getImagesArray($port->image)
                ?>
                <li>
                
                    @if(!empty($portImage))
                        @foreach($portImage as $portImg)
                            {{ HTML::linkRoute('gallery', $port->title)}}
                        @endforeach
                    @endif
                    
                </li>
            @endforeach
        </ul>
    </div>
</div>
mstnorris's avatar

@Ifrit there is no point worrying about blade view files yet as your Controller isn't even seeing the parameters you've passed.

What version of PHP are you using? Are you on a Mac, a PC? Are you using Homestead?

Ifrit's avatar

I'm on a PC and I'm using WAMP and it's using PHP 5.5

RomainLanz's avatar

You are displaying the route named gallery with only one parameter

 {{ HTML::linkRoute('gallery', $port->title)}}

but you route need 2 parameters

Route::get('/{title}/{id}', array('as' => 'gallery', 'uses' => 'HomeController@gallery'));
mstnorris's avatar

@Ifrit don't use the HTML facades, I used to use them but they just make things harder to read and in this case, debug.

Use standard HTML.

Also, make sure you're actually passing the right number of arguments to your URLs.

I asked earlier what URL you're hitting in the browser, so we can check that what you're doing correlates to what we expect that's going to happen.

mstnorris's avatar

That's not what your routes file is expecting though is it?

Your routes file is expecting

Route::get('/{title}/{id}', array('as' => 'portfolio', 'uses' => 'HomeController@portfolio'));

Yet you are trying to get

Route::get('bellamage/{title}/{id}', array('as' => 'portfolio', 'uses' => 'HomeController@portfolio'));

Please have a read through the basic routing documentation.

Also, if you're new to Laravel, check out the Quickstart Guide.

@Ifrit is this sorted?

1 like

Please or to participate in this conversation.