Daniel Del Core
Daniel Del Core

Reputation: 3221

Laravel Routing to controller actions

I've been trying for a while now to get laravel to route to my controller actions. I'm not sure of the best way to do this in the laravel framework.

How im picturing it in my head is like this.

I have a route setup to link to my controllers actions. so when i type domain.com/Home/Profile It will call the HomeController's profile action. which will do some processing and display the view on the page. ( like mvc4)

I may be going about this the wrong way but im just trying to avoid making a route for every view. I'm more after the correct method to this problem or whatever laravel devs do as a standard practice because it just feels strange to have a route for every view.

So far this is my code: routes.php

Route::get('/', 'HomeController@index');

Route::any('/{controller}/{action?}', function ($controller, $action = 'index') 
{
    $class = $controller.'Controller';
    $controller = new $class()

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

});

HomeController:

class HomeController extends BaseController 
{
    protected $layout = 'layouts.master';


    public function index()
    {
        $this->layout->content = View::make('index');
        return View::make('index');
    }

    public function profile()
    {
        $this->layout->content = View::make('profile');
    }
} 

Thanks for any help in advance.

Upvotes: 3

Views: 9749

Answers (3)

hook zou
hook zou

Reputation: 639

this code worked for me

$api->any('order_manage/{action?}', function ($action = 'index'){
        $app = app();
        $namespace = 'App\Http\Controllers';
        $controller = $app->make($namespace.'\ProjectOrderController');
        return $controller->callAction($action,[request()]);
    });

Upvotes: 0

Daniel Del Core
Daniel Del Core

Reputation: 3221

Found the answer that worked for me. It's Restful Controllers

I registered the controller with this line

Route::get('/', 'HomeController@index');
Route::controller('/', 'HomeController');

And changed my controller actions to getProfile()

Upvotes: 2

Wenzz
Wenzz

Reputation: 368

http://laravel.com/docs/controllers#resource-controllers

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

Upvotes: 0

Related Questions