MVC Framework
I have been coding and programming since 2004, and it all started with HTML and CSS. A couple of years later I wanted to make my own web applications and I dived into PHP and MySql.
Recently I have started to use a PHP Framework called Laravel, and it is amazing.
Laravel is MVC (Model - View - Controller) based, which basically means seperating code in logic ways to keep a good structure, and not repeating code.
- Model - Database and Queries
- View - Frontend (html, css, javascript etc.)
- Controller - Server. Communicate with View and Model
Getting Started
1) Installing a server and database
Before you install Laravel you need an PHP environment that actually can run Laravel. There are a couple of ways to do this.
- Laravel Homestead
- Xampp
- Wamp
I use Xampp on Windows 10. Xampp is easy to install and comes with PHP, MySql and PHPmyAdmin.
2) Installing Composer
Composer is a dependency manager for PHP. Make sure you install this program as well. Just go to getcomposer.org and follow the install instructions.
3) Installing Laravel
Once Composer is installed you are ready to install Laravel. Simply open your terminal window, go to the directory where you want to install Laravel and type this line of code (you may change "blog" to whatever you want to call your project):
composer create--prefer-dist laravel/laravel blog
(Note that this process may take some time.)
To check if everything works open your terminal window and make sure you are inside the correct folder (if not cd to your folder) then type
php artisan serve
Now you should see "Laravel development server started on http://localhost:8000/ in your terminal window.
Open your browser and go to that address. You should see "Laravel" written in the middle of the screen.
Before we go ahead let's take quick look at what we just wrote in our terminal window (php artisan serve). php artisan is simply a set of handy tools that comes with Laravel. Just type php artisan in your terminal window and it will list all of the available tools.
Folder structure and organizing files
Now that you have installed Laravel you will find a bunch of folders and files where you installed it (blog or whatever name you used), so where to start?
First we need to know where the Model, View and Controllers are located (MVC). Open your code editor (I'm using Sublime) and locate your main folder (blog or whatever).
Models
First let's take a look at the app folder, which is where all of your models are stored. By default Laravel create a file called user.php since most web applications have some sort of user authentication system. You don't have to use this file, it's just meant as a starting point and it will show you what a model look like. When you start to create your own models always use singular form and a capital letter in the file name (e.g. Post.php)
Views
To find the View files go to resources-->views. The view files are basically where all the html, css etc. goes. Laravel also create a file called welcome.blade.php which is what you see when you go to localhost:8000. It only contain som simple html and css. So why does the filename say blade.php? This is because it uses the Blade template engine, which means that you, in addition to regular php, can use "temlate tags" to keep your code super tidy. It is really easy to use, more on that later. You can also create new folders in the view forlder to keep your files more organized.
Controllers
To find your controllers you must go back to the app folder to app-->Http-->Controllers. Here you will find a file called controller.php, this is the base controller. You will also find a folder called auth, this contains controllers for basic authentication (login and registration). You can add as many folders as you like in the controller folder.
Creating static pages and understanding routes
There is no point using laravel to only create static pages but it is a simple way to learn how routing works. Let's create our first page.
Create a new file in the views folder and call it test.blade.php and then just paste the html code below into it.
<!DOCTYPE html>
<html>
<head>
<title>Laravel Test Page</title>
</head>
<body>
<h1>This is just a test page</h1>
</body>
</html>
So how do we access this page? A natural way to access this page would be http://localhost:8000/test but if you type this in your browser now you will only get an error telling you that the page could not be found. This is where routing comes in. We must tell Laravel what to do when someone comes to /test
Let's set up a route. Go to the app folder and open the routes.php file in your text editor. As you can see we already have a route for our welcome.blade.php file
Route::get('/', function () {
return view('welcome');
});
This tells Laravel to display the welcome (welcome.blade.php) file when someone goes to our main page (slash is the base url of our page). Now let's create a route for our test page. Just copy the code below and paste it below the welcome route (in routes.php):
Route::get('test', function () {
return view('test');
});
Now go to http://localhost:8000/test This code snippet tells Laravel to display/return the test.blade.php page we created. Normally we don't want functions in our routes file but rather point a route to a controller which will process the logic and then finally render the view from the controller so let's take a look at how we can do that.
This should get you started with the framework, and ready to start creating apps
This is a very great introduction to laravel..the laravel artisan tool is one of the most sophisticated command line tools in existence today. I would totally advise any php developer to jump right into laravel.
However,one thing I noticed is that the community is relatively weak. One of the setbacks I faced learning laravel was the unavailability of free pdfs and video tutorials online.
Important links to get started with are:
laracasts.com
learninglaravel.net
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
That's true, good links though. Thank you for a great comment.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Have used Laravel for some time now, one of the best frameworks if you decide to use PHP for a project
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit