Hello, I am new from Laravel, migrating from Codeigniter. And I am currently building my first website with Laravel.
At first, I am confused with Laravel's routing style. Do I really need to specify routes one-by-one? Is there any tricks to make Laravel routing into Codeigniter-like routing? I mean if I create a new controller named controller_a, then Laravel should able to render page to www.example.com/controller_a.
Also, how to fetch single or multiple $_GET parameters? Example: www.example.com/controller_a/param1/param2
How to load multiple views? This is my temporary solution:
$view = View::make('tpl_header');
$view .= View::make('hello');
$view .= View::make('tpl_footer');
return $view;
Is this okay?
Thank you.
Just my 2 cents. :D
I came from a CI background and had the same feeling as you when I first started to use Laravel. The question: Where did the dynamic routing go? After reading this from the guy who actually participate in developing CI, I started to re-think about auto-routing. http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil
Whether we should use auto-routing is still remained an open question. ( It is indeed some sugar in the beginning of the development ) The bottom line is that, automatic routing sometimes might lead to unexpected result when someone hits a route that isn't expected to be exposed.
Laravel routing starts to make sense to me because I have complete control on how the route should look.
Back to the question you have asked.
// Static pages, NO controllers involved.
Route::get('{page}.html', function($page) {
// views/static/*.blade.php
$dir = 'static.';
if(View::exists($viewPath = $dir.$page)) {
return View::make($viewPath);
}
else {
// views/errors/not_found.blade.php
return View::make('errors.not_found');
}
});
// I personally DO NOT recommend this usage.
// As this approach is considerably dangerous and tedious to "simulate" the same action like CI
// For controller-like usage, briefly. NOT practical and Validation required.
Rout::get('{controller}/{action}', function($controller, $action) {
$controller = ucwords($controller).'Controller';
App::make($controller)->{$action}();
});
A better approach could be
// In this case, Route order matters, if you have the above static pages route setup.
Route::controller('user', 'UserController');
UserController.php
class UserController extends BaseController {
public function getIndex()
{
}
public function postIndex()
{
}
}
Route::get('foo/{arguments?}', function() {
$segments = Request::segments();
// Do whatever you want in $segments array
})->where('arguments', '(.*));
2 and 3) Yeah I reading more documentation on Laravel website and looks like I need to migrate my coding style. Goodbye CI, then. LOL.
Thanks.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community