Skip to content

Instantly share code, notes, and snippets.

@leohmoraes
Forked from nickdenardis/laravel-tips.markdown
Created December 5, 2017 19:21
Show Gist options
  • Save leohmoraes/45351ce554304c57f13dc31eee780acc to your computer and use it in GitHub Desktop.
Save leohmoraes/45351ce554304c57f13dc31eee780acc to your computer and use it in GitHub Desktop.
Tips on using Laravel

Resources

https://github.com/eightygrit/deploy-laravel/blob/master/deploy-laravel.z.sh
https://github.com/onigoetz/deployer
https://gist.github.com/purwandi/4110547
https://gist.github.com/chipotle/5506641

Add Sublime Text 2 to the CLI

sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

Use MAMP PHP CLI

export PATH=/Applications/MAMP/bin/php/php5.3.6/bin/:$PATH

Install Composer without cert errors

curl -ksS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Create a new project with composer

composer create-project laravel/laravel appname

Add the needed packages package.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "require": {
    	"laravel/framework": "4.0.*",
    	"way/generators": "dev-master",
        "zurb/foundation": "dev-master",
    	"zurb/foundation": "dev-master",
    	"conarwelsh/mustache-l4": "dev-master",
	},
    "require-dev":{
    	"phpunit/phpunit": "3.7.*",
        	"mockery/mockery": "0.7.*"
	},
"autoload": {
	"classmap": [

Mustache Reference

https://github.com/conarwelsh/mustache-l4

Update the package installation

composer update

Add the items to the app dependancies app/config/app.php

...
'Way\Generators\GeneratorsServiceProvider',
'Conarwelsh\MustacheL4\MustacheL4ServiceProvider',
...

Change the bootstrap bootstrap/start.php

hostname
$env = $app->detectEnvironment(array(
        'local' => array('your-machine-name'),
));

Set the storage to be writable

chmod -R 777 app/storage/

Edit ths app/config/database.php

Use 127.0.0.1 for locahost if possible

Edit the app/config/view.php (if using mustache for JS too)

'paths' => array(__DIR__.'/../../public/views'),

Create/Edit the public/package.json (if using mustache for JS too)

{
    "name": "laravel4-and-foundation",
    "version": "0.0.1",
    "private": true,
    "dependencies": {
            "foundation": "*",
            "mustache": "*"
        }
}

Install the new frontend needs (if using mustache for JS too)

cd public
npm install
cd ..

Create all the frontend asset folders (if using mustache for JS too)

mkdir public/views public/views/layouts public/js public/css

Download Foundation SCSS and install to directory

https://github.com/zurb/foundation/tree/scss-standalone

JSON API

sudo chmod -R 755 vendor/way/generators/src/Way/

Generate some resources

php artisan generate:resource webpage --fields="url:string"

Add the v1 route

//create a group of routes that will belong to APIv1
Route::group(array('prefix' => 'v1'), function()
{
    //... insert API routes here...
	Route::resource('webpages', 'V1\WebpagesController'); //notice the namespace
});

Move the controllers to the namespace

mkdir app/controllers/V1
mv app/controllers/WebpagesController.php app/controllers/V1/

app/controllers/WebpagesController.php

<?php
//use our new namespace
namespace V1;
 
//import classes that are not in this new namespace
use BaseController;
 
class WebpagesController extends BaseController {

Add the respository

mkdir app/repositories

composer.json

"autoload": {
    "classmap": [
      "app/commands",
      "app/controllers",
      "app/models",
      "app/database/migrations",
      "app/database/seeds",
      "app/tests/TestCase.php",
      "app/repositories"
    ]
},

Seed the database

<?php
class WebpagesTableSeeder extends Seeder {
    public function run()
	{
		// Uncomment the below to wipe the table clean before populating
		DB::table('webpages')->truncate();

		$webpages = array(
			 array(
				'url'    => 'http://wayne.edu/',
				'created_at' => date('Y-m-d H:i:s'),
				'updated_at' => date('Y-m-d H:i:s'),
			),
			array(
				'url'    => 'http://wayne.edu/about/',
				'created_at' => date('Y-m-d H:i:s'),
				'updated_at' => date('Y-m-d H:i:s'),
			),
		);

		// Uncomment the below to run the seeder
		DB::table('webpages')->insert($webpages);
	}
}

dump composer each time you add migration files

composer dump-autoload

Run the migrations and seed

php artisan migrate --seed

To run tests

vendor/phpunit/phpunit/phpunit.php

Remove the default test

rm app/tests/ExampleTest.php

Make tests for the controllers and repositories

mkdir app/tests/controllers app/tests/repositories

Tests should all fail now

Note: in TDD, the objective is to do no more work than is required to make your tests pass. So we want to do the absolute bare minimum here.

Create an app errors file

touch app/errors.php

Move the frontend Foundation assets

cp -R vendor/zurb/foundation/scss public/
cp -R vendor/zurb/foundation/js/ public/js/

Move the resource templates into the public dir

mv app/views/webpages public/views/

Deploy with capistrano

sudo gem install capistrano
capify .
cap deploy:setup
cap deploy:check
cap deploy

Capdeploy file

# This makes a few assumptions:
#
#   - You are using a similar .gitignore to Laravel's default, so your
#     vendor directory and composer(.phar) are not under version control
#   - Composer is installed as an executable at /usr/local/bin/composer
#
# If you don't have Composer installed globally, you can modify the
# appropriate task (:composer_install). Or make your life simpler and
# install Composer globally.

# Fill in application-specific configuration options below

set :application, "Laravel 4 Testing Roll"
set :repository,  "git@github.com:nickdenardis/laravel-test.git"

set :scm, :git
set :scm_username, "nickdenardis"

set :user, "user"

role :web, "server"
role :app, "server"
role :db,  "server", :primary => true

set :deploy_to, "/usr/local/www/sites/base/"
set :deploy_via, :remote_cache

set :use_sudo, false  # I don't need sudo, but you might
set :ssh_options, {:forward_agent => true}
set :copy_exclude, [".git", ".gitignore", ".tags", ".tags_sorted_by_file"]
set :keep_releases, 5

# Laravel deployment
namespace :deploy do

  task :update do
    transaction do
      update_code
      copy_config
      composer_install
      link_shared
      laravel_migrate
  	  symlink
    end
  end

  task :finalize_update do
    transaction do
      # No need to change ownership
      #run "chmod -R g+w #{releases_path}/#{release_name}"
    end
  end

  task :symlink do
    transaction do
      run "ln -nfs #{current_release} #{deploy_to}/#{current_dir}"
    end
  end

  task :link_shared do
    transaction do
      run "ln -nfs #{shared_path}/system #{current_release}/public/system"
    end
  end

  task :laravel_migrate do
    transaction do
      run "php  #{current_release}/artisan migrate"
    end
  end

  task :laravel_rollback do
    run "php  #{deploy_to}/#{current_dir}/artisan migrate:rollback"
  end

  task :restart do
    transaction do
      # set writable storage dir
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/cache ]; then chmod -R 777 $mydir/cache; rm -f $mydir/cache/*; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/database ]; then chmod -R 777 $mydir/database; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/meta ]; then chmod -R 777 $mydir/meta; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/logs ]; then chmod -R 777 $mydir/logs; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/sessions ]; then chmod -R 777 $mydir/sessions; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/views ]; then chmod -R 777 $mydir/views; rm -f $mydir/views/*; fi"
      run "mydir=\"#{deploy_to}/#{current_dir}/app/storage\";if [ -d $mydir/work ]; then chmod -R 777 $mydir/work; fi"
    end
  end

  task :composer_install do
    transaction do
      run "cd #{current_release};/usr/local/bin/composer install"
    end
  end

  # This task lets you keep server-specific configuration files in "shared/config/".
  # Any file there will just be copied to your app/config directory.
  task :copy_config do
    transaction do
      #run "cp #{shared_path}/config/* #{current_release}/app/config/"
    end
  end

end

after "deploy:rollback", "deploy:laravel_rollback"

https://help.github.com/articles/deploying-with-capistrano https://github.com/capistrano/capistrano/wiki/2.x-from-the-beginning http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html https://github.com/stefanooldeman/capistrano-handbook/wiki/Anatomy-of-a-capistrano-installation http://ryanflorence.com/deploying-with-capistrano-without-rails/

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