Laravel routing

2019-09-03 04:16发布

问题:

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.

回答1:

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}();

});



回答2:

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'
));


回答3:

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]+'))


回答4:

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...