Skip to content

Instantly share code, notes, and snippets.

@WISHPRO
Forked from alemohamad/laravel-5.md
Last active August 29, 2015 14:16
Show Gist options
  • Save WISHPRO/90c937f1eeffe110f35a to your computer and use it in GitHub Desktop.
Save WISHPRO/90c937f1eeffe110f35a to your computer and use it in GitHub Desktop.

Contents

  1. Laravel packages
  2. Testing
  3. Server Tools
  4. Markdown
  5. Laravel Homestead
  6. Setup Laravel 4 in a Shared Hosting with securing Laravel base file
  7. Laravel Application Environments
  8. Install ImageMagick
  9. Hooking up a custom domain name to DigitalOcean
  10. Laravel Repository (IoC)
  11. Testing in Laravel
  12. Front stuff
  13. Laravel Books
  14. Cloud Hosting Services
  15. Git Hosting Services
  16. Online Playgrounds
  17. Other

Laravel packages

Laravel 5

Testing

Server Tools

Markdown

Laravel Homestead

# 1. install Vagrant and VirtualBox
# https://www.vagrantup.com/
# https://www.virtualbox.org/

# 2. add homestad box into vagrant
$ vagrant box add laravel/homestead

# 3. create Code folder
$ mkdir Code
$ cd Code

# 4. clone the homestead repository
$ git clone https://github.com/laravel/homestead.git homestead

# 5. prepare your yaml config file
$ cd homestead
$ vim Homestead.yaml

# 6. if you haven't, create your keys (this will be used in authorize and keys in the yaml file) (optional)
$ ssh-keygen -t rsa -C "your@email.com"

# 7. update your hosts file with your custom url
$ sudo vi /etc/hosts

# 8. start your virtual machine in the homestead folder
$ vagrant up

#################
# OTHER ACTIONS #
#################

# visit the website: http://example.app:8000/

# enter the VM in the homestead folder
$ vagrant ssh

# suspend the VM before turning off your computer
$ vagrant suspend
# and then you can do "vagrant up" to use the VM again

# to reload the VM in the homestead folder
$ vagrant reload

# to add a virtual host into homestead VM
## Easy method
$ vagrant reload --provision
## Advanced method
$ vagrant ssh
$ serve new-domain.app /home/vagrant/Code/path/to/public/directory

# to update the version of homestead
$ vagrant box update

# create a new laravel project
$ composer create-project laravel/laravel <project-name>

# create a new laravel project with a custom version
$ composer create-project laravel/laravel=4.2.* <project-name> --prefer-dist

# change permissions of storage folder
$ sudo chmod -R 777 app/storage

# play with artisan, so we don't have to create/edit php files
$ php artisan tinker

# copy the ssh public key
$ pbcopy < ~/.ssh/id_rsa.pub

Use HHVM in Laravel Homestead

Setup Laravel 4 in a Shared Hosting with securing Laravel base file

# 0. This assume that you have file structure something like this:
/home/username/public_html

# 1. Create new folder outside public_html:
/home/username/main-laravel

# 2. Move all the main file of Laravel (app, boostrap, vendor, composer.json, composer.lock, phpunit.xml etc) into that folder (step 1) **except Public folder

# 3. Open /home/username/main-laravel/bootstrap/paths.php and edit to look like this:
## replace
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../public',
'base' => __DIR__.'/..',
'storage' => __DIR__.'/../app/storage',
## to
'app' => __DIR__.'/../../main-laravel/app',
'public' => __DIR__.'/../../public_html/laravel',
'base' => __DIR__.'/../../main-laravel',
'storage' => __DIR__.'/../../main-laravel/app/storage',

# 4. Now create a new folder inside public_html
/home/username/public_html/laravel

# 5. Now, move all the content in public folder of Laravel into that folder (step 4)

# 6. Open /home/username/public_html/laravel/index.php and edit to look like this:
## replace
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
## to
require __DIR__.'/../../main-laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../../main-laravel/bootstrap/start.php';

# 7. Now create .htaccess in /home/username/public_html and insert this code:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^laravel
RewriteRule ^(.*)$ laravel/$1 [L]

# Now, your laravel website can be access at http://username.com

Laravel Application Environments

bootstrap/start.php

$env = $app->detectEnvironment( function ()
{
    return getenv('APP_ENV') ?: 'development';
});

.env.development.php

<?php

return [
    'DB_PASSWORD' => 'secret password'
];

// add this file to .gitignore

// .env.php has the production environemnt keys (it exists only in production)
// (if the hosting can't define env vars, this should be the way)

How to retrieve each env variables in our project?

getenv('DB_PASSWORD');

Install ImageMagick

Just ssh into your vm, then install the required packages, like so:

vagrant ssh
sudo apt-get update
sudo apt-get install imagemagick
sudo apt-get install php5-imagick

Hooking up a custom domain name to DigitalOcean

Hostname Record Type Value
@ A [ip-address]
  •    | A           | [ip-address]
    

www | CNAME | [url-address]

  1. The main domain (it can be the alias).
  2. An alias to point every subdomain to the ip-address.
  3. Point a subdomain to the main domain.

If you can't do advanced DNS config

  1. Go to https://cloud.digitalocean.com/domains
  2. Click on “Add Domain”
  3. Add the domain in the “Name” field
  4. Add the IP in the “IP Address” field
  5. Select the droplet where you have that domain with that IP address
  6. Click on “CREATE DOMAIN”, and that's it.
  7. Then add the DigitalOcean DNS in your domain provider DNS config
ns1.digitalocean.com
ns1.digitalocean.com
ns1.digitalocean.com

Laravel Repository (IoC)

app/Acme/LessonRepositoryInterface.php

<?php namespace Acme;

interface LessonRepositoryInterface {
  public function recent();
}

app/Acme/DbLessonRepository.php

<?php namespace Acme;

class DbLessonRepository implements LessonRepositoryInterface {
  public function recent()
  {
    return Lessons::take(5)->orderBy('id', 'desc')->get();
    // you can also add this as a method inside the Lessons model
  }
}

Add respository files to Composer

"autoload": {
  "classmap": {
    "app/Acme"
  }
}

Then do a composer dump.

app/routes.php

<?php

App::bind('Acme\LessonRepositoryInterface', 'Acme\DbLessonRepository');
Route::resource('lessons', 'LessonsController');

app/controllers/LessonsController.php

<?php

use Acme\LessonRepositoryInterface as LessonRepository;

class LessonsController extends BaseController {
  
  protected $lesson;
  
  public function __construct(LessonRepository $lesson)
  {
    $this->lesson = $lesson;
  }
  
  public function index()
  {
    return $this->lesson->recent();
  }
  
  // ...
}

Testing in Laravel

Don't always extend TestCase

class ExampleTest extends PHPUnit_Framework_TestCase {
  // use only PHPUnit for business logic
}

class ExampleTest extends TestCase {
  // use complete Laravel TestCase logic
}

Do an acceptance test with Laravel

<?php

class ExampleTest extends TestCase {
  public function test_displays_home_page()
  {
    $this->call('GET', '/');
    $this->see('Hello world', 'h1');
  }
  
  protected function see($text, $element = 'body')
  {
    $crawler = $this->client->getCrawler();
    $found = $crawler->filter("{$element}:contains('{$text}')");
    
    $this->assertGreaterThan(0, count($found), "Expected to see {$text} within the view");
  }
}

Test a controller that use a repository (IoC)

<?php

class LessonsControllerTest extends TestCase {
  
  public function setUp()
  {
    parent::setUp();
    
    // prepare the repository mock for use in every controller test
    $this->lesson = Mockery::mock('Acme\LessonRepositoryInterface');
    App::instance('Acme\LessonRepositoryInterface', $this->lesson);
  }
  
  public function tearDown()
  {
    Mockery::close();
  }
  
  public function test_binds_lessons_to_index_view()
  {
    $this->lesson->shouldReceive('recent')->once()->andReturn('foo');
    
    $result = $this->call('GET', '/lessons');
    
    $this->assertEquals('foo', $result->getContent());
  }
  
}

Improve performance with test database in memory

You can add a configuration file in app/config/testing/database.php:

<?php

return [
  'default' => 'sqlite',
  'connections' => array(
    'sqlite' => array(
      'driver' => 'sqlite',
      'database' => ':memory',
      'prefix' => '',
    )
  )
];

And then create an example test that uses a test database in memory:

<?php

class ExampleTest extends TestCase {
  
  public function setUp()
  {
    parent::setUp();
    Artisan::call('migrate');
  }
  
  public function test_it_works()
  {
    // in orther for this test to work
    // we have to create an Order model and its migration :)
    Order::create(['name' => 'Wii U']);
    
    $this->assertsEquals('Wii U', Order::first()->name);
  }
  
}

Front stuff

Laravel Books

Cloud Hosting Services

Git Hosting Services

Online Playgrounds

Other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment