M
M
mergenich2014-05-13 22:15:57
Laravel
mergenich, 2014-05-13 22:15:57

Laravel: how to make routes like in Kohana?

Kohan has this default route:

Route::set('default', '(<controller>(/<action>(/<id>)))')
  ->defaults(array(
    'controller' => 'welcome',
    'action'     => 'index',
  ));

There is a default controller/method, the parameters in brackets (<controller>(/<action>(/<id>)))are optional, they are executed if they exist and they are in the url.
for example.com - welcome controller, index method
for example.com/question - question controller, index method
for example.com/question/new - question controller, new method
How to do this in Laravel 4?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-05-13
@mergenich

no way. As far as I remember, symfony/routing doesn't allow this, and that's a good thing.

V
Vyacheslav Plisko, 2014-05-14
@AmdY

Write the last route something like

Route::any('{controller}/{action?}', function($controller, $action = 'index') {
    return call_user_func_array([App::make(ucwords($controller). 'Controller'), $action], array_slice(func_get_args(), 2));
});

M
mergenich, 2014-05-14
@mergenich

I found a very convenient way for me.
In the routes we write the name of the controller.
Method names start with get and post

class QuestionController extends BaseController {
    public function getIndex() {    // example.com/question
        //
    }
    //  для POST запроса на адрес example.com/question/new
    public function postNew() {    
        //
    }
    public function getItem($id) {    // example.com/question/item/1
        //
    }

RESTful Controllers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question