Tuesday, September 3, 2019

In Laravel 5, What's the difference between url route and asset ?

Consider the type of URL that is needed / how the URL is being used. One of the advantages of having separate helper methods for each type of URL is they can have different handling logic. For example, assets (e.g. CSS, images, etc.) could involve a check that the file exists in the file system but do not require the type of analysis that a route would because the route may have parameters.

url() Generates an absolute URL to the given path (code)

  • Use for static URLs (which should be rare).
  • Accepts array of parameters that are encoded and added to the end of the domain.
  • Preserves any URL query string.
    {{ url('search') }}
    // http://www.example.com/search
    
    {{ url('search', ['qevo', 'laravel']) }}
    // http://www.example.com/search/qevo/laravel

asset() Generates a URL to an application asset (code)

  • Use for files that are directly served such as CSS, images, javascript.
  • Only accepts a direct path.
    {{ asset('css/app.css') }}
    // http://www.example.com/css/app.css

route() Gets the URL to a named route (code)

  • Use for every route (every route should be named to help future-proof path changes).
  • Requires named routes.
  • Accepts associative array for route parameters.
  • Allows override for relative route vs. absolute route (default).
    {{ route('user.profile', ['name'=>'qevo']) }}
    // http://www.example.com/user/qevo/profile
    
    {{ route('user.profile', ['name'=>'qevo'], false) }}
    // /user/qevo/profile

Laravel textare editor : Using Summernote WYSIWYG editor with Laravel

Summernote is a WYSIWYG (What You See Is What You Get) style editor which offers lots of nifty features such as pasting images from clipboard, interactive text editing, easy server integration and it has all the features for standard HTML formatting.
I recently had to use summernote in a Laravel based project at work, and I must say it was a breeze to set up and get running. Also, the Summernote project is very active and has over 6000 stars on GitHub which makes me happy as it means better future maintainability and support. Now let me show you how you can implement Summernote with Laravel.

Set up routes:

  • We define routes for displaying summernote form, storing the summernote content and finally to display the content by fetching it from the database.
  • Note that the Route::view method is new in laravel 5.5 and if you are using an older version then you can use a traditional Route::get and return the view in a method callback.

Summernote input form:

  • Create a summernote.blade.php and add the form which is going to display our summernote input.
  • We will include summernote related assets and initialize summernote on our textarea input which has a class of summernote on document ready,
  • My Summernote view:summernote_laravel_tutorial

Parse and store Summernote content:

  • Generate a model along with a resourceful controller and a migration file.
  • Add a longText content field to the summernote migration so that it can suffice for a decent length of content.
  • Don’t forget to generate summernote table from the migration file.
  • Now we will parse our summernoteInput in the store method of SummernoteController.
    • We use domdocument() method to generate a native PHP dom object which makes it easier to navigate HTML in a logical manner.
    • Also, the Summernote encodes any images in the content to base64 format which means easy transfer from client to server but is not feasible if you intend to store the content in a database. As even a small image encoded in base64 format could add tens of thousands of characters to the content.
    • We work around this by decoding the base64 format image (using base64_decode()) and saving it into the public directory. We then replace the src attribute of the image in the content with the link to the image we just stored.
    • We finally store the content in DB and return a view to display the content.

Fetch and display content from the view:

  • In summernote_display.blade.php add the following code to display the content.
  • Yeah, a single {!! $summernote->content !!} is enough!. Note that we use {!! !!} and not {{ }}as the latter escapes the string which we do not want in this case.
  • This is how mine looks like:

    https://www.kerneldev.com/2018/01/11/using-summernote-wysiwyg-editor-with-laravel/