AIPD TECH AIPD TECH - 7 months ago 63
PHP Question

Laravel 5.2 routing like cakephp

I am new to laravel. I am coming form cakephp, so routing is creating a bit of difficulty for me.

I have tried the Question but getting error in that.
I also tried for

Route::controller();
and
Route::resource();
but not getting the the result i want.

I simply want the rounting to be

http://example.com/controller/action/param1/param2/param3


also, if i can get answer for the backend management like

http://example.com/backend/controller/action/param1/param2/param3

Answer

In Laravel 5.2 the use of Route::controller('Controller') has been deprecated due to annoying race conditions.

To get your desired result. Let's say you have a controller App\Http\Controllers\MyController.

In your routes.php file you would have the following:

Route::group(['middleware' => ['web']], function(Router $router) {

    // Note the question marks after the parameters
    // this makes them optional.
    $router->get('uri/{action?}/{param1?}/{param2?}', [
        'uses' => 'MyController@getIndex'
    ]);
});

You would now have a controller method getIndex

// As the parameters were optional, make sure to give them
// default values.
public function getIndex($action = null, $param1 = null, $param2 = null)
{
    // Your route logic
}
Comments