laravel and artisan cli

21
LARAVEL AND ARTISAN CLI Sayed Ahmed Computer Engineering, BUET, Bangladesh MSc. Computer Science, U of Manitoba, Canada http://sayed.justetc.net

Upload: patch

Post on 14-Feb-2016

83 views

Category:

Documents


6 download

DESCRIPTION

Sayed Ahmed Computer Engineering, BUET, Bangladesh MSc. Computer Science, U of Manitoba, Canada http://sayed.justetc.net. Laravel and Artisan CLI. Introduction Usage Common artisan commands New artisan commands also come with packages Laravel 4 generators package will be discussed - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Laravel  and  Artisan CLI

LARAVEL AND ARTISAN CLI

Sayed AhmedComputer Engineering, BUET, BangladeshMSc. Computer Science, U of Manitoba, Canadahttp://sayed.justetc.net

Page 2: Laravel  and  Artisan CLI

TOPICS Introduction Usage

Common artisan commands New artisan commands also come with

packages Laravel 4 generators package will be

discussed How to develop new artisan commands

Page 3: Laravel  and  Artisan CLI

SHOULD YOU USE THEM In general, it’s better to use Will make your development faster However, you may also consider the aspect

Just the command is there, do you really need to use it Just because, you can generate something, are you doing for

that or there is a need (business or technical) Is there time for it?

Justify the need, justify the time assigned for the project, justify budget, justify usefulness, also what is most important right now

Use for the benefit; do not use just to use it But you need to learn it all

Page 4: Laravel  and  Artisan CLI

WHAT IS ARTISAN A command-line interface Comes with Laravel Provides helpful commands

That facilitates rapid application development It is driven by the powerful Symfony Console

component But Artisan is not that difficult

If you have to use it, You have to know it for sure, But knowing is not that difficult. You can just read and know

So relax, you will see

Page 5: Laravel  and  Artisan CLI

LARAVEL - USAGE php artisan list

List all available commands php artisan help migrate

Help screen for the command migrate php artisan migrate --env=local

Run the migrate on local development platform

php artisan –version Artisan version

Page 6: Laravel  and  Artisan CLI

ARTISAN COMMANDS CAN BE EXTENSION/PACKAGE SPECIFIC Install this package, you will get more commands

https://github.com/JeffreyWay/Laravel-4-Generators New generators as we get from the package

generate:model generate:controller generate:seed generate:view generate:migration generate:resource generate:scaffold generate:form generate:test generate:pivot

Page 7: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS COMMANDS Related Artisan Commands

php artisan generate:migration create_posts_table

php artisan generate:migration add_user_id_to_posts_table

Page 8: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS ARTISAN COMMANDS

php artisan generate:migration create_posts_table --fields="title:string, body:text“

php artisan generate:migration destroy_posts_table

php artisan generate:migration destroy_posts_table --fields="title:string, body:text“

Page 9: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS ARTISAN COMMANDS

php artisan generate:migration remove_completed_from_tasks_table --fields="completed:boolean“

php artisan generate:model Post php artisan generate:view dog php artisan generate:view index --

path=views/dogs php artisan generate:seed dogs

Page 10: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS ARTISAN COMMANDS

php artisan generate:resource dog --fields="name:string“

php artisan generate:resource dog --fields="name:string, age:integer“

php artisan serve php artisan generate:scaffold tweet --

fields="author:string, body:text“ php artisan generate:form tweet

Page 11: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS ARTISAN COMMANDS

php artisan generate:form tweet --method="update“

php artisan generate:form tweet --html="div“

# copy the output to the clipboard php artisan generate:form tweet |

pbcopy # save it to a form partial php artisan

generate:form tweet > app/views/posts/form.blade.php

Page 12: Laravel  and  Artisan CLI

LARAVEL-4-GENERATORS ARTISAN COMMANDS

php artisan generate:test FooTest php artisan generate:pivot posts tags php artisan migrate php artisan generate:migration

create_posts_table --fields="title:string, description:text"

php artisan generate:migration create_tags_table --fields="name:string"

php artisan generate:pivot posts tags

Page 13: Laravel  and  Artisan CLI

DEVELOPING NEW ARTISAN COMMANDS

By default the commands are in the app/commands folder

You can store your commands in other locations as you want However, the location has to autoload by

composer.json settings

Page 14: Laravel  and  Artisan CLI

DEVELOPING NEW ARTISAN COMMANDS php artisan command:make FooCommand

Create a new command class php artisan command:make FooCommand --

path=app/classes --namespace=Classes Generate command class in a user provided path

php artisan command:make AssignUsers --command=users:assign Specify the term to be used from command line

php artisan command:make AssignUsers --bench="vendor/package“

If you need to create the command for a package

Page 15: Laravel  and  Artisan CLI

INSIDE THE CLASS FOR THE COMMAND

Name and description properties Provide them for help

Fire method will be triggered The getArguments and getOptions

methods are to accept the command line parameters for the command to be created

How options are passed to a command php artisan foo --option=bar --option=baz

Page 16: Laravel  and  Artisan CLI

RETRIEVING OPTIONS AND ARGUMENTS Retrieving options and arguments from

command line To be used inside your command class And then you can process as you want

$value = $this->option('name'); $arguments = $this->argument();

Page 17: Laravel  and  Artisan CLI

CALLING OTHER COMMANDS Calling other commands inside from

your commands $this->call('command:name',

array('argument' => 'foo', '--option' => 'bar'));

Page 18: Laravel  and  Artisan CLI

MESSAGE TO THE USER After the intended operation, you may

want to display something to the user $this->info('Display this on the screen'); $this->error('Something went wrong!');

Page 19: Laravel  and  Artisan CLI

ASKING THE USER FOR INFO Asking the user for info in between of

the intended operation $name = $this->ask('What is your

name?'); $password = $this->secret('What is the

password?'); $this->confirm($question, true);

Page 20: Laravel  and  Artisan CLI

REGISTERING COMMANDS• Once you are done writing your commands

Done in the app/start/artisan.php Use Artisan::add to register the method

You need to register it Artisan::add(new CustomCommand); Artisan::resolve('binding.name');

Artisan::resolve('binding.name'); Registering A Command That Is In The

IoC Container