Dieses Blog durchsuchen

Dienstag, 23. August 2016

Symfony3: create a console command and test it

Sometimes you need an easy way to create a console command to add some automation and some admintools to your application. With Symfony3 it is rediculous simple

Install symfony3

Open a console and execute:
$ composer require symfony/symfony

Create a project

Create a project "consoletest" by executing following command on the terminal
$ symfony new consoletest.

Now you have a new project created.

Add your command

Create a file "CreateUserCommand.php" on folder "src/AppBundle/Command" and add following Code
As you can see, this simple command extends from Class Command and implements only 2 methods.
The configure command sets up the command, setup name, description and , prints out helptexts if you want

The execute method does the final work and runs your commandcode. You can manipulate databases, filesystems, get a servicereponse or configure your application with it.

Run your command

At next you can test your command by typing :
$ bin/console app:create-user

You should see your helptext,



Awsome. Isn´t it?

Create a unittest for your command

With symfony it´s very easy to test your new command. Here is a simple test for it:
- Create a folder and a file "test/Appbundle/CreateUserCommandTest.php" with following content in it

And now you can run
$ phpunit symfony

Montag, 22. August 2016

ES6: prevent that = this;

Some days ago I found a solution for following problem in Javascript:

Solve a really ugly problem

I know many lines of code where you find follwing that-this reference
Now with ES6 you can solve this problem with the arrow function like this.

As you can see, you can directly use this in the arrow-function, which is a kind of a shortcut to "function". Thats really nice.

Sonntag, 21. August 2016

node.js: create a contactform with express, jade, boostrap and node-mailer

Today we want to create a contactform and send a message via gmail.
To make the form look good, we add bootstrap to hour project and we are using express to setup the webserver. At the end we want to use node-mailer to send the email. This might sound complicated, but you only have to follow my copy & paste instructions and everythings will be easy.

Prerequisits

At first you need to have some tools installed. here they are.
This should be familliar to you

node.js installed
npm installed
a gmail account with username and passsword
Installed and working express. For that please follow the last tutorial >>here<<.

After that, you have express installed, jade added to express and a routing added

Add the jade contactform

Jade is a templatelanguage like jsx, phptal, velocity etc. There are some facts, which seems to be nice, like the shortened syntax. But there might be some contras against jade too. For now we want to be open for new technologies and give jade a try.

If you want to get more information about jade, visit: http://jade-lang.com/

Create a file named "contact.jade" to your views folder. If you do not have a views folder, create one in your projectfolder and place following code to the contact.jade file.


Create the routes for your form

Open your app.js and add the contact-routes.(See comments in following code).
One route to render the form, and one route to send the email. My complete app.js looks like that

This will bind a route "contact" and "contact/send" to your express webserver app. the simple "contact" route only renders the form.

The more complex route "contact/send" will do a lot of stuff more. At first, it will create a transporter object, which tells the mailer to send the email via googles gmail over a predefined "from account". After that, it defines the mailoptions with the plaintextversion, the htmlversion, the sender and the recipient

At the end, the sendMail function gets called on the transporter object. This will simply send the mail and do the errorhandling. If an error occures, we will log it to the console.


Please make sure to set your own gmailaccount in the mailoptions and in the sendEmail function.

Add bootstrap to the project

Download and extract bootstrap to following folderstructure:
DownloadUrl: getboostrap.com

<yourprojectroot>
|__public
    |__css
    |__js
    |__fonts


create a file layout.jade with following code
This will add the baselayout with a little navigation   

Add a homepage to your project

Create a file named "index.jade" and add it to your view folder


Start your webserver

go to your projectroot and run
$ node app.js

Use your contactform

Now you can surf http://localhost:3000/contact and fill out the form.
After submitting your form you will get a "message send" to your console and you will recieve your email in your gmail account


If your mail gets rejected......

To test your email sending,  you have to allow "less secure apps" in your gmail testaccount:

You can do this by following :

https://support.google.com/accounts/answer/6010255?hl=enhttps://support.google.com/accounts/answer/6010255?hl=en







Samstag, 20. August 2016

node.js: create your first express webserver

In the last tutorial, we have created a simple webserver. That was great as preperation to use the expressframework, which can handle the webserverstuff for us. Let's have a look to express.

Prerequisits

node.js installed
npm installed

Install express globaly

$ npm install -g express

Create a npm project

Create your project rootfolder "express-project" and open a terminal in that folder and typpe:
$ npm init

Follow the intstructions on your terminal.

Add needed dependencies

Open package.json and add your dependencies. My package.json looks like that:

You can just pick the "dependencies" object 

 

Add you dependencies

Create a file "app.js" and paste following code
This will reference your dependencies and setup your express webserver.
As you can see, the webserver will be bound on port 3000 and it will return a simple "Hello world" response and will log the same words to the console.

Start the webserver

Open a terminal in the projectroot and type:
$ node app.js 


Now you can surf http://localhost:3000 and you should see "Hello world"

Thats it

Freitag, 19. August 2016

node.js: create a little bit more complex webserver

In the last tutorial, we have created a simple webserver. Lets create a little bit more complex webserver with node.js, which can serv different filetypes and check for errors on the filesystem.

Prerequisits

node.js installed
npm installed

Create a webserver


This webserver can dispatch a hand full of images , text, javascript and css

Create a file "server.js" and paste following code

This will create the webserver

Create a file to dispatch

Open a terminal in the projectroot and create a file "index.html" with following content
 



Start the webserver

Open a terminal in the projectroot and type:
$ node server.js 


Now you can surf http://localhost:1337/index.html and you should see "Testing"

Thats it

node.js: create the simplest webserver

Let's create a simple webserver with node.js

Prerequisits

node.js installed
npm installed

Create project

Type follwing command in your terminal and follow the instruction
$ npm init


This will create a package.json, which contains all the needed information for your project

Create a webserver


A webserver is just a piece of code in that case, wich allows us to send a message to the users browser.

Create a file "server.js" and paste follwing code

This will require the package http, which contains the server functions.
Then it will bind the server on localhost on port 3000. After that It will send a static returnvalue "Hello world" to the browser, wenn a user calls http://localhost:3000

Start the webserver

Open a terminal in the projectroot and type:
$ node server.js 


Now you can surf http://localhost:3000 and you should see "Hello World"

Thats it

Donnerstag, 18. August 2016

codeception: install on ubuntu 16.04

Today we want to give codeception a try. The installation is pretty simple

Prerequisits

composer. If you do not have composer installed, you can do it >>here<<

php >5.3 with mbstring enabled

Install codeception

create a folder "/var/www/html/codeception"

open a terminal and type:
$ composer require codeception/codeception

Add codeception to your terminal
$ sed -i "$ aalias codeception='/var/www/html/codeception/vendor/bin/codecept'" ~/.bashrc

Reload bash
. ~/.profile


That's pretty much it. Now you can use it.

Type "codeception" to get all options