28
loading...
This website collects cookies to deliver better user experience
composer.json
in the root of your project.composer create-project laravel/laravel Laravel-From-Scratch-Blog-Project
in a directory/folder where you would like to place your project. Understand the structure of this command. Here laravel/laravel
is the GitHub repo name and the final parameter is how you would like to name your project.cd Laravel-From-Scratch-Blog-Project
and run the artisan command php artisan serve
composer create-project
command. An alternate solution is to install the Laravel installer tool and then create projects with even simpler command Laravel new PROJECT_NAME
.composer global require laravel/installer
.vendor/bin
directory to your system $PATH
variable. It means the Installer will then work globally and will be not tied to a particular project folder.cd
into your project folder and run the command php artisan serve
. Now see your app in the browser at http://127.0.0.1:8000
http://<project-folder>.test
using Laravel Valet. route::get('/', function() {
return view("welcome);
});
/
is always meant for homepage) is received, a function is called which returns a view (HTML) whose name is welcome..blade.php
extension with the view file welcome in the route definition. It is because Laravel automatically recognize it as a blade file.In real Laravel apps we use bundling tools like Laravel Mix for compiling CSS and JavaScript files. But at this stage we create CSS and JS files directly in the public folder and then include in our welcome.blade.php
Here we start building the basic structure of our blog. A blog page that lists blog posts titles and excerpts. Blog titles are linked to pages that contains the full post.
file_get_contents()
function. We use route wildcard to find which file to pick and pass to the view.Route::get('/posts/{post}', function ($slug) {
//
})->where('post', '[a-zA-Z\-]+');
Here the route wildcard {post}
will accept only alphabets, both lowercase and uppercase, and a hyphen. The plus sign means one or more.
You can find other helper functions like whereAlpha()
, whereAlphaNumeric()
, and whereNumber
file_get_contents()
each time a blog post is viewed seems wasteful, particularly if a lot of users access your blog at the same time. So, why not cache the blog posts for performance?/**
* Closure function
*/
$post = cache()->remember('posts'.$slug, 1200, function() use ($path) {
return file_get_contents($path);
});
/**
* Or using an arrow function like this (PHP7.4 or above)
*/
$post = cache()->remember('posts'.$slug, 1200, fn() => file_get_contents($path));