phplaravellaravel-3laravel-routing

Laravel 3 Bundle Routing Variables Failing


We want to make our web service API more RESTful, so I'm trying my hand at routing variables. It seems simple, but I'm getting a 404 error...

It's a Laravel 3 project, and I'm trying to define a new route.
Right now, our URL's look like: api/object/v1/find?some=thing&another=thing.

My goal is to have them look more like: API/v2/objects/{numericID}.

Everything works fine when testing with this...

<?php

Route::get('v2/companies', function() {

return 'Hello';

});

This too...

Route::get('v2/companies/id', function( $id = 5678 ) {
    return print_r($id, TRUE);;
});


But when I try:

Route::get('v2/companies/{id}', function( $id = 5678 ) 
{
    return print_r($id, TRUE);
});

..it all goes to hell.
Calling a URL like \api\v2\companies\1234 throws a 404.

What gives??


Solution

  • You need to use the set wildcard patterns for routes in Laravel 3...

    Forcing a URI segment to be any digit:

    Route::get('user/(:num)', function($id)
    {
        //
    });
    

    Allowing a URI segment to be any alpha-numeric string:

    Route::get('post/(:any)', function($title)
    {
        //
    });
    

    Catching the remaining URI without limitations:

    Route::get('files/(:all)', function($path)
    {
        //
    });
    

    Allowing a URI segment to be optional:

    Route::get('page/(:any?)', function($page = 'index')
    {
        //
    });