Daniel Del Core
Daniel Del Core

Reputation: 3251

Laravel routing

I'm new to laravel and I'm having trouble with routing.

What I'm trying to do is use my routes like so

Domain/{controller}/{action }

So that I can go to the home's test page

Home/test

The documentation doesn't really make sense to me so if anyone could point me in the right direction that would be great. Currently I'm hardcoding my routes for every page.

Upvotes: 0

Views: 244

Answers (4)

BigfishDim
BigfishDim

Reputation: 56

Basically you could start with:

Route::get('Domain/{controller}/{action?}', function ($controller, $action = 'index') {

    $class = $controller.'Controller';
    $controller = new $class()

    return $controller - > {
        $action
    }();

}) -> where(array('controller' = > '[a-z]+', 'action' = > '[a-z]+'))

And you can change Route::get with Route::any to catch it all

Other simple tested example (should be working:)

Route::any('{controller}/{action?}', function($controller, $action = 'index') {

$class = $controller . 'Controller';
$controller = new $class();
return $controller->{$action}();

});

Upvotes: 2

Mathlight
Mathlight

Reputation: 6663

You can setup the routing like this:

Route::get('/orders/nieuw', 'OrdersController@nieuw');
Route::post('/orders/nieuw', 'OrdersController@save_nieuw');
Route::get('/orders/edit/{orderId}', 'OrdersController@edit');
Route::post('/orders/edit/{orderId}', 'OrdersController@save_edit');
Route::get('/orders/show/{idPartij}', 'OrdersController@show');
Route::get('/orders/show_row/{idPartij}/{row_id}', 'OrdersController@show_row');
Route::get('/orders/{active?}', 'OrdersController@index');

Simplified version:

Route::get is simply saying: When we receive an get route, which meets the conditions, do something.

The condition ('/orders/nieuw' is in an URL translated to http://www.domain.com/orders/nieuw

If that conditions is met, where going to use the OrdersController and execute the function nieuw ( which is an public function inside the OrdersController class)

The same works for post requests.

Params can you pass with {param}. And if you have an not necessary param you can use {param?}

If you need more info, let me know

EDIT 1 if you read this, then you should say that the following is the clossest as it can get:

Route::get('home/test');
Route::get('home/test2');
Route::resource('home', 'HomeController');

That way, you only have to declare the controller 1 time, and the non standard links...

Upvotes: 0

Priya jain
Priya jain

Reputation: 703

Basic GET Route

Route::get('controller/method', function()
{
 //
});

Basic POST Route

Route::post('controller/method', function()
{

});

Named Route:

Route::get('controller/action', array('uses' =>'Controller@method'));

Suppose it is your form action:

{{ Form::open(array('route' => 'register.store')) }}

then you can write the code for routing as:

Route::get('register', array(
  'uses' => 'RegisterController@index',
  'as' => 'register.index'
));
Route::post('register', array(
  'uses' => 'RegisterController@store',
  'as' => 'register.store'
));

Upvotes: 2

prava
prava

Reputation: 3986

I do not have much knowledge on Laravel framework. But overall this should work:

Route::get('Domain/{controller}/{action}', function($controller, $action)
{
    //
})
->where(array('controller' => '[a-z]+', 'action' => '[a-z]+'))

Upvotes: 0

Related Questions