Reputation: 2785
Im trying to switch from using code igniter to laravel however I noticed in every tutorial I'd followed, we always declare the route in route.php in laravel unlike in code igniter that it has a default routing like http://localhost/projname/controller/method
. Is there a way to have an auto routing like CI or I'd just missed something in laravel routing rules?This is very important because we all know big websites have more than 50 links and it will be a hustle if we are going to declare those all in routes.php in laravel.
Upvotes: 9
Views: 2843
Reputation: 153
//create controller name like UserController
//Steps:
// 1. route matched to either post or get request,
// 2. used web/controllerName/MethodName/Parameter1/parameter2 ..
// all parameters received in an array as $params. web/ is used like a route
// prefix. If no method is passed it will call index method
// 3. explode the parameter
// 4. called the controller with method and paramteres passed
// 5. parameters are matched for regex allowing alphanumeric and slash (url)
// 6. passed through guest middleware
// created controller as mentioned below:
// class SomeController extends Controller
// {
// public function index($param1,$param2,$param3){
// return 'index'.$param1.$param2.$param3;
// }
// }
Route::match(['get','post'],'/web/{controller}/{method?}/{params?}', function ($controller, $method='index', $params='') {
$params = explode('/', $params);
$controller = app()->make("\App\Http\Controllers\\". ucwords($controller).'Controller' );
return $controller->callAction($method, $params);
})->where('params', '[A-Za-z0-9/]+')->middleware('guest');
Upvotes: 0
Reputation: 1094
Is there a way to have an auto routing like CI
Why yes there is. In your route file do Route::controller(Controller::detect());
Now in your controller class make sure each function name is concatenated with action_
. So if your function name is homepage()
then make it action_homepage()
Keep in mind that you can use restful controller names get_homepage()
and post_homepage()
. But you'll have to declare this class variable in your controller public static $restful = true;
Upvotes: 9