Video Tutorial
Routes
Laravel has a very powerful routing engine, you can use right out of the box, and what it pretty much does is when you request a URL for e.x: larv-cms.dev/about you handle this request by creating a route listener and sends a response back, it’s very simple as well as it is very important for either large-scale projects or smaller ones
So for handling HTTP Request (Pretty Much Routing) open Web.php under Routes folder on your main app directory and add to it your route listener, and redirect it (route it) to the page you want to, for us we are creating a test Route /test so when the user hits larv-cms.dev/test we are going to respond with a Hello Word! we will talk about returning (redirecting) to an actual HTML/PHP Page, using the view helper and blade engine, So the Code should look like this:
//We are using a HTTP GET Request here, you can use all of the Supported HTTP Requests and Responses
Route::get('/test', function() {
return "Hello World!";
});
So by typing the URL above larv-cms.dev/test you will see a Static Text Hello World on the page that means that our routing engine works and we can start swimming in a Bit Larger sea.
We will implement more advanced Routing very soon as we dive on this Tutorial Series.
Views (Blade Engine)
While you are creating a web-based application (a simple blog in our case) in most cases you want to show (render) HTML/PHP on the request of a page (for e.x: /dashboard) so for this approach we are going to use views and rendering the page using laravel blade template engine, it is as simple as calling the helper function view(‘directory.bladeFile’) but before this, we need to create the PHP file first, so go to resources/views feel free to make a subdirectory for a bit of Organizing in our case we create a /pages and we name a new file thereby dashboard.blade.php (make sure to append blade.php so it knows that you are using blade engine (Beleive me it is way much better than you think))
and start building your web page using HTML (I guess that you know that already) watch the video tutorial above for further information.
Now change:
... function() {
return "Hello World!";
} ...
With
... function() {
return view(pages.dashboard);
} ...
here you may notice two things seems wrong but no, First Laravel Uses. (Dot) for Directory Listening rather than / (Forward Slash), Second we are not including the whole file name only (dashboard) that because laravel recognize this automatically, and we are using the view() function to reference the file. as simple as that, now refresh your page /test or better change the route name to dashboard (More Reasonable).
Controllers
A Controllers is pretty much the File Gonna Handle all your logic from getting Database Records and Saving them to Controlling your routes and views, so Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes.
you can create a Controller using Artisan, throw this Command:
#Make Sure you are in the main Directory of your app
php artisan make:controller PagesController --resource
the — resource option will setup few commonly used functions on your controller so you can use them right out of the box.
Now go to the index() function (remember that you can use any function name you like) but this seems suitable for the case, under the body return a view pretty much like:
public function index() {
return view('pages.dashboard');
}
Now change the Route under Web.php replace the callback function with a Controller name
Route::get('/dashboard', 'PagesController@index')->name('dashboard.index');
we specify which function we are pointing to the route by @functionName and we set a name for our route for easily accessing it from other files on our application.
Now you can Refresh or by going to larv-cms.dev/dashboard you should see your dashboard page pretty much the same as before but now it is being done in a bit more advanced way using a controller, which means you are using laravel in the right way.
By The End of this Tutorial you can build your app but only using static pages, and no database which makes no sense but still working fine.
What Next
Make Sure you Watch the Next Videos in the Series for More Advanced Topics and Making a Larger/Scalable Applications with laravel.
We will cover Models and Database Tables Manipulation as well as the .env file and more advanced ones, so stay tunned.
Thanks For Being Patient.