- Forums
- laravel
- Creating New Laravel Project With A Route, A Controller And Blade Template
this is a simple tutorial for beginners who want to learn how to create a new simple laravel project, the project contains the basics such as a custom route, a controller and a blade template. This is a fundamental elementary step by step guide. [4609], Last Updated: Mon Jun 24, 2024
el_lara
Wed Oct 26, 2016
0 Comments
628 Visits
Simple Laravel Project
Follow this tutorial to start a new simple project with a custom route, a controller and a blade template. i wrote this tutorial because i am a beginner and this will help me get started in understanding laravel. these are the requirements:
1. in this examle, i will be using laravel version 5.3 which is the latest version today.
2. you must have php already installed, you will need php version 5.6.24 or higher. i recommend, version 7, but i will be using version 5.6.24 in this tutorial
3. you must have composer already installed
if you have all three requirements, lets continue. for this tutorial, i want to create an about page, its nothing fancy, just a simple basic html. the goal is to show the about page using this url: http://localhost:8000/about
1. Create Project Command
open your command and cd to the folder where you want to create your project. In my example, i have a XAMPP server running on my PC. and my project are located in this folder: C:\apachefriends\xampp\htdocs\opensource\laravel\tutorial\ so this is the command i would use:done
create-project laravel/laravel NewProject
2. cd to NewProject Folder
cd C:\apachefriends\xampp\htdocs\opensource\laravel\tutorial\NewProject
3. Start Server
Use this command to start the new server using artisan
php artisan serve
4. Confirm Server Running
open in browser:
http://localhost:8000 - you should see the default laravel page. [advanced] You can also view the page is you are running XAMPP http://localhost/opensource/laravel/tutorial/NewProject/public/
5. Open Another Terminal
Now that you have the server running, open a second terminal.
cd C:\apachefriends\xampp\htdocs\opensource\laravel\tutorial\NewProject
6. Create Controller
create a controller with command.
php artisan make:controller pagesController
7. Open Project
I am using sublime, so i will now open the folder as a project in sublime, if you are not using sublime, you can skip this step
8. Create about Page
create a new pages folder in resources/views/pages/ or you can also create with command:
mkdir resources/views/pages
9. Create Controller
create a new about page in the pages folder, you can also use a command to create a new file:
LINUX OR MAC
touch resources/views/pages/about.blade.php
WINDOWS
copy NUL > resources/views/pages/about.blade.php
NOTE: if you are using windows, you might get an error, its ok if you do, the system will create the file, this is the error i got:
Copy-Item : Cannot find path 'C:\apachefriends\xampp\htdocs\opensource\laravel\tutorial\NewProject\NUL' because it does
not exist.
At line:1 char:5
+ copy <<<< NUL > resources/views/pages/about.blade.php
+ CategoryInfo : ObjectNotFound: (C:\apachefriend...\NewProject\NUL:String) [Copy-Item], ItemNotFoundExce
ption
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
11. Open about.blade.php
open resources/views/pages/about.blade.php to add the HTML in our blade template file.
<h1>About Page</h1><hr>This is about.blade.php file
12. Open pagesController.php
open app/Http/Controllers/pagesController.php, add the following function and save
public function about(){
return view('pages.about');
}
13. Create Route
now lets add the routes so when we open the url /about it will display the contents from about.blade.php.
10. Open web.php
open the file called web.php, add the following code and save.
Route::get('about', 'pagesController@about');
these are another alternative ways you can write the code for a route:
Route Example 2
Route::get('users/create', ['uses' => 'exampleController@create']);
14. See In Browser
open the about page in browser: http://localhost:8000/about
You should see the content your put in about.blade.php from step 11
The End