Laravel 4 : Route to localhost/controller/action

前端 未结 3 2025
终归单人心
终归单人心 2020-12-03 15:43

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 th

相关标签:
3条回答
  • 2020-12-03 16:28
    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();
        });
    
    0 讨论(0)
  • 2020-12-03 16:34

    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"];
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-03 16:47

    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');
    
    0 讨论(0)
提交回复
热议问题