Domanda

I'm more or less new to Laravel 4. I've never used routes before but normally what I'm used to is url/controller/action and then the backend routing for me. I've read the documentation for routes and controllers a few times as well as read through some tutorials and so, I'm trying to figure out how to get this to work without writing a route for every controller and action.

I tried something like

Route::get('{controller}/{action}', function($controller, $action = 'index'){
    return $controller."@".$action;
});

Now then, I know this is wrong since it doesn't work, but what am I missing? On most tutorials and stuff I'm seeing an route for more or less every controller and action like:

Route::get('/controller/action' , 'ControllerName@Action');

Which seems silly and like a waste of time to me.

Is there anyway to achieve what I want?

È stato utile?

Soluzione

If you are looking for a more automated routing, this would be the Laravel 4 way:

Route:

Route::controller('users', 'UsersController');

Controller (in this case UsersController.php):

public function getIndex()
{
    // routed from GET request to /users
}

public function getProfile()
{
    // routed from GET request to /users/profile
}

public function postProfile()
{
    // routed from POST request to /users/profile
}

public function getPosts($id)
{
    // routed from GET request to: /users/posts/42
}

As The Shift Exchange mentioned, there are some benefits to doing it the verbose way. In addition to the excellent article he linked, you can create a name for each route, for example:

Route::get("users", array(
    "as"=>"dashboard",
    "uses"=>"UsersController@getIndex"
));

Then when creating urls in your application, use a helper to generate a link to a named route:

$url = URL::route('dashboard');

Links are then future proofed from changes to controllers/actions.

You can also generate links directly to actions which would still work with automatic routing.

$url = URL::action('UsersController@getIndex');

Altri suggerimenti

app\
    controllers\
        Admin\
           AdminController.php
        IndexController.php
Route::get('/admin/{controller?}/{action?}', function($controller='Index', $action='index'){
        $controller = ucfirst($controller);
        $action = $action . 'Action';
        return App::make("Admin\\{$controller}Controller")->$action();
    });

Route::get('/{controller?}/{action?}', function($controller='Index', $action='index'){
        $controller = ucfirst($controller);
        $action = $action . 'Action';
        return App::make("{$controller}Controller")->$action();
    });

I come from .Net world and routing is typically done:

/{Controller}/{action}/{id}

Which looks like:

/Products/Show/1 OR /Products/Show/Beverages

In Laravel I accomplish this routing like so:

Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='index', $id = null) {
    $controller = ucfirst($controller);
    return APP::make("{$controller}Controller")->$action($id);
});

The controller would look roughly like so:

class ProductsController extends BaseController {
    public function Show($id) {
        $products = array( 1 => array("Price" => "$600","Item" => "iPhone 6"),
                           2 => array("Price" => "$700", "Item" => "iPhone 6 Plus") );

        if ($id == null) {
            echo $products[1]["Item"];
        } else {
            echo $products[$id]["Item"];
        }

    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top