Sunday, November 27, 2016

Swagger implementation in laravel 5

Hi Everybody

Today we will learn how to integrate swagger into laravel 5.

Swagger creates the RESTful contract for our API, detailing all of its resources and operations in a readable format for easy development and integration.

Installation

For Swagger 2.0

 composer require "darkaonline/l5-swagger:~3.0"  

Open your AppServiceProvider (located in app/Providers) and add this line in register function

  $this->app->register(\L5Swagger\L5SwaggerServiceProvider::class);  

open your config/app.php and add this line in providers section

 \L5Swagger\L5SwaggerServiceProvider::class  

Configuration

 Run php artisan l5-swagger:publish to publish everything  
 Run php artisan l5-swagger:publish-config to publish configs (config/l5-swagger.php)  
 Run php artisan l5-swagger:publish-assets to publish swagger-ui to your public folder (public/vendor/l5-swagger)  
 Run php artisan l5-swagger:publish-views to publish views (resources/views/vendor/l5-swagger)  
 Run php artisan l5-swagger:generate to generate docs or set generate_always param to true in your config or .env file  

Now open your controller and write following script above class definition to define basic information about your api.

 /**  
  * @SWG\Swagger(  
  *   schemes={"http","https"},  
  *   host="dev.eduru.com",  
  *   basePath="/",  
  *   @SWG\Info(  
  *     version="1.0.0",  
  *     title="This is my website cool API",  
  *     description="Api description...",  
  *     termsOfService="",  
  *     @SWG\Contact(  
  *       email="contact@mysite.com"  
  *     ),  
  *     @SWG\License(  
  *       name="Private License",  
  *       url="URL to the license"  
  *     )  
  *   ),  
  *   @SWG\ExternalDocumentation(  
  *     description="Find out more about my website",  
  *     url="http..."  
  *   )  
  * )  
  */  
 class ProductController extends Controller  


Write following script above your function where you have defined your api functionality.

 /**  
    * @SWG\Get(  
    *   path="/api/v1/products",  
    *   description="Returns all pets from the system that the user has access to",  
    *   operationId="findPets",  
    *   produces={"application/json", "application/xml", "text/xml", "text/html"},  
    *   @SWG\Parameter(  
    *     name="tags",  
    *     in="query",  
    *     description="tags to filter by",  
    *     required=false,  
    *     type="array",  
    *     @SWG\Items(type="string"),  
    *     collectionFormat="csv"  
    *   ),  
    *   @SWG\Parameter(  
    *     name="limit",  
    *     in="query",  
    *     description="maximum number of results to return",  
    *     required=false,  
    *     type="integer",  
    *     format="int32"  
    *   ),  
    *   @SWG\Response(  
    *     response=200,  
    *     description="pet response",  
    *     @SWG\Schema(  
    *       type="array",  
    *       @SWG\Items(ref="#/definitions/pet")  
    *     ),  
    *   ),  
    *   @SWG\Response(  
    *     response="default",  
    *     description="unexpected error",  
    *     @SWG\Schema(  
    *       ref="#/definitions/errorModel"  
    *     )  
    *   )  
    * )  
    */  
 /**  
    * Display a listing of the resource.  
    * 
    */  
   public function getProducts()  
   {  
     //  
   }  


Now run follwoing command to publish.

 php artisan l5-swagger:publish  

Now open following link in your browser

http://yourdomain.com/api/documentation

In above link, we can see our api documentation and its implementation.

Thanks

Sunday, November 20, 2016

Filesystem in laravel

Hi

Today we will learn, how to store file in laravel.

Laravel framework comes with built in storage handling which is compatible with a variety of systems. It is built upon the popular library Flysystem and therefore integrates with lots of services via the same common interface.

Storage configuration resides in the “config/filesystems.php” file. We can add multiple engines such as local drives and cloud drives.

Basic file operations

The filesystem integration provides a straight forward way of interacting with files. Basic write/read operations are illustrated below.

 // Write content to file  
 $content = 'File content ...';  
 Storage::put( 'my_file.txt', $content );  
 // Read content from file  
 $content = Storage::get( 'my_file.txt' );  
 // Delete file  
 Storage::delete( 'my_file.txt' );  
 You can also append/prepend content easily to existing files.  
 // Append content to file  
 $append = 'Appended text...';  
 Storage::append( 'my_file.txt', $append );  
 // Prepend content to file  
 $prepend = 'Prepended text...';  
 Storage::prepend( 'my_file.txt', $prepend );  

Listing files and directories

Sometimes we need to read your file structure which can be done with the listing methods.
We can list both files and directories in one level or recursively.

 // List all files in directory  
 $files = Storage::files($directory);  
 // List all files & directories in directory  
 $files = Storage::allFiles($directory);  
 // List all directories in directory  
 $directories = Storage::directories($directory);  
 // List all directories recursively in directory  
 $directories = Storage::allDirectories($directory);  


Thanks

Thursday, November 10, 2016

Laravel: Touching Parent Timestamps

Hi

Today we will learn how to use Touch property/method in Laravel Eloquent.

Sometime we need to update parent's model timestamp when the child model is updated.

When a model belongsTo or belongsToMany another model, such as a Comment which belongs to a Post, it is sometimes helpful to update the parent's timestamp when the child model is updated.

For example, when a Comment model is updated, we may want to automatically "touch" the updated_at timestamp of the owning Post. Laravel Eloquent makes it easy. We need to add a touches property containing the names of the relationships to the child model.

 <?php  
 namespace App;  
 use Illuminate\Database\Eloquent\Model;  
 class Comment extends Model  
 {  
   /**  
    * All of the relationships to be touched.  
    *  
    * @var array  
    */  
   protected $touches = ['post'];  
   /**  
    * Get the post that the comment belongs to.  
    */  
   public function post()  
   {  
     return $this->belongsTo('App\Post');  
   }  
 }  

Now, when we update a Comment, the owning Post will have its updated_at column updated as well:

 $comment = App\Comment::find(1);  
 $comment->text = 'Edit to this comment!';  
 $comment->save();  

Thanks