Laravel New Project Steps

Here are my steps for creating a new laravel project. I know the docs are excellent but these are the steps specific to my setup, which is probably pretty common. For reference, I use the laravel installer to create new projects and I use Homestead. I already have Homestead set up so it’s just a matter of changing a few things and the inevitable updates, since this is something I don’t do frequently (thus the need for notes).

1. update laravel installer
composer global update "laravel/installer"

2. create the laravel project
laravel new projectname
cd projectname

3. Update Homestead
cd into your Homestead directory and edit the Homestead.yaml file
Add a new entry to the sites section

4. run
vagrant reload --provision

5. Update your hosts file to add projectname
open -a "Sublime Text" /etc/hosts
use .test extension (I’ve had issues with Chrome and .dev or .app extensions not working)

6. Launch the vagrant box
vagrant up

7. SSH into Homestead and cd into the projectname directory
run
composer install

9. Create a .env file in your project root
Copy everything from .env.example into .env

10. Generate the Application Key from Homestead (all php artisan commands have to be run from Homestead on my setup)
php artisan key:generate

11. Navigate to your projectname.test in the browser – you should get the Laravel welcome page

Installing the Html and Form Builder package back into Laravel 5

Here’s how to get the Form and Html Builder package back when using Laravel 5.

composer require illuminate/html

Then, in /config/app.php add the following to the providers and aliases arrays:

'providers' => [
	...
 
	'Illuminate\Html\HtmlServiceProvider',
],
 
'aliases' => [
 
	...
 
	'Form'=> 'Illuminate\Html\FormFacade', 
	'HTML'=> 'Illuminate\Html\HtmlFacade',
],

 

Disable Registration in Laravel 5

A new Laravel 5 installation comes with user registration out of the box. If you don’t want to use this feature, here is a clean way to disable it. Open up app\Http\Controllers\Auth\AuthController.php and add the following methods:

public function getRegister()
{
    return redirect('auth/login'); // Or wherever
}

public function postRegister()
{

}

These will override the parent methods in the AuthenticatesAndRegistersUsers trait.

Using Laravel 5 Auth Middleware

To restrict pages in your app to authentication status, add the middleware to the controller’s constructor.

To restrict a page to guests only (not signed in):

public function __construct()
{
    $this->middleware('guest');
}

For signed in users:

public function __construct()
{
    $this->middleware('auth');
}

If you want to make exceptions for certain methods, just pass those parameters in as an array for the second argument.

$this->middleware('auth', ['except' => 'show']);

 

Laravel Project Setup

First, get composer if you don’t already have it (Windows): https://getcomposer.org/Composer-Setup.exe

I like to use the composer installer (as opposed to the Laravel Installer). Open command prompt and move into www directory (or wherever you want your project to live).

composer create-project laravel/laravel name-of-project

 

This will create the directory “name-of-project” and place all the laravel files inside at the same time.

Create a database. Use utf8_unicode_ci for the collation.

Next, open up the .env file located at the project root and edit the settings for your project. If using Git, go ahead and edit the .gitignore to add your IDE specific files and any other files you want to ignore. (Tip for Phpstorm users: the .idea folder is not visible within the directory tree inside the IDE. You only see it if you view the files from outside Phpstorm.)

Run

php artisan migrate

to migrate the included migrations.