Reputation: 5452
As a beginner at Laravel 5 programming i am trying to develop a basic application in order to learn Laravel 5 fundamentals. In my application, i have a couple of pages and i am trying to route them as:
Route::get('/', 'PagesController@index');
Route::get('/vehicles', 'PagesController@vehiclepage');
Route::get('/calculation', 'PagesController@calculationpage');
Route::get('/settings', 'PagesController@settingspage');
PagesController:
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('index');
}
public function vehiclepage()
{
return view('vehiclepage');
}
public function calculationpage()
{
return view('calculationpage');
}
public function settingspage()
{
return view('settingspage');
}
}
Some form process will be goin on in these pages ass well. I wonder, is there a more efficient way to have a better routing for my pages ? Any help would be appreciated.
Upvotes: 1
Views: 7893
Reputation: 1155
The implicit controller routes may be fine for you if u wish,as said by Kaloyan Doichinov. your route.php file will be like Route::controller('/','yourControllerName');
and ur controller method should be prepand with http verb,like eg index() => getIndex(){} etc
so for post request use => postMethodName(){} //change MethodName with ur method name.
and so on for put,delete etc requests.......
Upvotes: 0
Reputation: 1253
you can do route-model binding (google it) that you can ignore using wildcards such as
pagename/{id}
you can use Route::resource()
see: http://laravel.com/docs/5.0/controllers#restful-resource-controllers
Upvotes: 0
Reputation: 2979
Your routing is perfectly fine, although there are alternatives. For example, you may define your routing like so:
routes.php
Route::get('login', ['uses' => 'LoginController@index']);
Route::controller('/', 'WelcomeController');
WelcomeController.php
class PagesController extends Controller {
public function getIndex()
{
return view('homepage');
}
public function getVehicle()
{
return view('vehiclepage');
}
public function getCalculation()
{
return view('calculationpage');
}
public function getSettings()
{
return view('settingspage');
}
}
What this will do is route GET requests to your.domain/login to the index
method of your LoginController
(assuming you will have one), and if the parameter is anything else, it will try to find a method that starts with get
and ends with the word being requested, in the PagesController
, then serve it, so a GET request to your.domain/settings will end up in the getSettings
method of PagesController
. You can read more about how this all works in the docs
Note - I'm not saying that this method is better - some people say that "monolithic" routers are the Devil, while others (like me) prefer to have all the logic in the controller and have the routing defined in the most condensed way possible. It all comes down to personal preference and style. Just keep in mind that consistency is key - whichever approach you prefer, stick to it!
Upvotes: 2
Reputation: 4676
1st of all if you want to access those pages at main route at your site as : /{pagename} the best way is to stay as you have it.. list them all 1 by 1. Its not good to have dynamic matching router for the main site route "/".
If its ok for you to set some prefix to this route as: '/page/{pagename}' here is a nice dynamic loader..
class PagesController extends Controller {
public function show( $slug)
{
$pageslug = 'page.' . (string)$slug;
// This means that your views must be in views/page/ folder
if( view()->exists($pageslug)){
return view($pageslug);
}
abort(404);
}
}
And your route will be:
Route::get('/page/{slug}', 'PagesController@show');
At end all views in views/page/XXXX will be accessible at /page/{viewname}
Upvotes: 2
Reputation: 875
I had same question, because I was coming from KOHANA FW and then I did in my Laravel project like this :
Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='defaulmethod', $id = null) {
$controller = ucfirst($controller);
return APP::make("{$controller}Controller")->$action($id);
});
It worked when url was like this :
/{Controller}/{action}/{id}
but i think your route system is not bad, it is very easy to understand and if you don't make big project - you can use it.
Upvotes: 1
Reputation: 127
I'm new to laravel also, I think you made the things in the right way. But just a small advice, try to keep your code very clean for your future use, like that :
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('index');
}
public function vehiclepage()
{
return view('vehiclepage');
}
public function calculationpage()
{
return view('calculationpage');
}
public function settingspage()
{
return view('settingspage');
}
}
As the public function vehiclepage is new function and it is not a part of the public function index.
Upvotes: 0
Reputation: 168
Try this
Route::get('{page}', function($page) {
return View::make($page);
});
Upvotes: 0