<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Santhosh</title>
    <description>The latest articles on DEV Community by Santhosh (@santhoshj).</description>
    <link>https://dev.to/santhoshj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F224148%2Fe25b8426-1b06-4275-ba01-7b4b2ec4fad5.png</url>
      <title>DEV Community: Santhosh</title>
      <link>https://dev.to/santhoshj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/santhoshj"/>
    <language>en</language>
    <item>
      <title>Unable to set request header in laravel tests</title>
      <dc:creator>Santhosh</dc:creator>
      <pubDate>Fri, 27 Mar 2020 16:45:39 +0000</pubDate>
      <link>https://dev.to/santhoshj/unable-to-set-request-header-in-laravel-tests-42el</link>
      <guid>https://dev.to/santhoshj/unable-to-set-request-header-in-laravel-tests-42el</guid>
      <description>&lt;p&gt;I have a test which is trying to login and get details. I need to pass a bearer token in request header for this operation. Please see the below code. I could see that the header seldom has the headers that I set. Can anyone give me a pointer to fix this issue?&lt;/p&gt;

&lt;p&gt;I am using Laravel 7.2.2, PHP 7.4, &lt;/p&gt;

&lt;p&gt;And I am running php artisan test&lt;/p&gt;

&lt;p&gt;Code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function a_user_can_get_details()
    {
        $this-&amp;gt;create_user();

        $response = $this-&amp;gt;json('POST', 
                    '/api/login', 
                    [
                        "email" =&amp;gt; "john.doe@example.com",
                        "password" =&amp;gt; "john123"
                    ]);
        $response-&amp;gt;assertSuccessful();

        $token = Str::replaceLast('"', '', Str::replaceFirst('"', '', $response-&amp;gt;getContent()));
        $headers = [
                        'Accept' =&amp;gt; 'application/json',
                        'Content-Type' =&amp;gt; 'application/json',
                        'Authorization' =&amp;gt; 'Bearer ' . $token
                    ];


        $response = $this-&amp;gt;withHeaders($headers)
                        -&amp;gt;get('api/user');

        $response-&amp;gt;assertSuccessful();
        $this-&amp;gt;assertCount(1, User::all());

    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And here is the error I am getting. Actually, the test must pass. That is the right user name and password:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Response status code [403] is not a successful status code. Failed asserting that false is true.

  at tests/Feature/UserTest.php:141
    137|
    138|         $response = $this-&amp;gt;withHeaders($headers)
    139|                         -&amp;gt;get('api/user');
    140|
  &amp;gt; 141|         $response-&amp;gt;assertSuccessful();
    142|         $this-&amp;gt;assertCount(1, User::all());
    143|
    144|     }
    145|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Written with &lt;a href="https://stackedit.io/"&gt;StackEdit&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>help</category>
    </item>
    <item>
      <title>How to continuously run a Laravel queue listener on server</title>
      <dc:creator>Santhosh</dc:creator>
      <pubDate>Thu, 31 Oct 2019 10:27:40 +0000</pubDate>
      <link>https://dev.to/santhoshj/how-to-continuously-run-a-laravel-queue-listener-on-server-1oic</link>
      <guid>https://dev.to/santhoshj/how-to-continuously-run-a-laravel-queue-listener-on-server-1oic</guid>
      <description>&lt;p&gt;Laravel has a built-in queue management system which will help the main processing thread to ward off time taking asynchronous processes to a separate thread, also it can help the application to be scaled horizontally by adding multiple listeners/processors to simultaneously consume the queue.&lt;/p&gt;

&lt;p&gt;In a development enviornment, we can listen to a queue by simply running &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan queue:listen
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Where as, when we deploy this to a server, we cannot use this command. Because when we close the session, artisan command will exit. Earlier days, I was using screen command on linux to keep it running. Somehow, screen is a messy way to handle this situation.&lt;/p&gt;

&lt;p&gt;To avoid this, we need to use a supervisor application to keep the artisan command alive.&lt;/p&gt;

&lt;p&gt;There are many tools available to this purpose. A few of them are: forever, hypervisor, and pm2.&lt;/p&gt;

&lt;p&gt;Here I am sharing the way how you can run artisan command using pm2.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 start artisan --name QueueName --interpreter php -- queue:work --daemon
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Explaining this, pm2 start will initialize artisan command. PM2 can start a named instance using --name, here QueueName will be the name of the instance. By default, commands will be run using node interpreter. So, --interpreter will set it to php. Finally, 'queue:work --daemon' is the argument to the php artisan command.&lt;/p&gt;

&lt;p&gt;After running the above command from the laravel project root, run the following command to check the status.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 list
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C7goXhnn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/krffzsh637de3h43rvhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C7goXhnn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/krffzsh637de3h43rvhc.png" alt="PM2 screen grab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Status must be online. If it is not, run&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pm2 monit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;to see the logs and take corrective actions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Written with &lt;a href="https://stackedit.io/"&gt;StackEdit&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>laravel</category>
      <category>discuss</category>
      <category>php</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to create audit trail for laravel models with custom user table</title>
      <dc:creator>Santhosh</dc:creator>
      <pubDate>Sun, 27 Oct 2019 15:48:41 +0000</pubDate>
      <link>https://dev.to/santhoshj/how-to-create-audit-trail-for-laravel-models-with-custom-user-table-1fl0</link>
      <guid>https://dev.to/santhoshj/how-to-create-audit-trail-for-laravel-models-with-custom-user-table-1fl0</guid>
      <description>&lt;p&gt;Wherever there are entities with multiple states we would need to keep track of the changes made to the entity. Eg: A purchase order is drafted, then, quantity of the items are changed. Purchase order is sent to vendor. Purchase order is completed. Here, business owners will be interested in knowing 'Who' changed 'What' and 'When' of these changes.&lt;/p&gt;

&lt;p&gt;There is an awesome package which can be used with Laravel to acheive this with minimal effort - &lt;strong&gt;Laravel Auditing&lt;/strong&gt;. Minimum requirement is Laravel 5.8 or higher as of when I am writing this post.&lt;/p&gt;

&lt;p&gt;Let me explain this with a blog example.&lt;/p&gt;

&lt;p&gt;Create a new project&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project laravel/laravel blog
cd blog
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Create a new model for Posts and Comments&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:model Post -mcr
php artisan make:model Comment -mcr
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;-mcr&lt;/em&gt; option will create a migration and a resource controller for the model.&lt;/p&gt;

&lt;p&gt;Now let us install the laravel Auditing package.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require owen-it/laravel-auditing 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above command will install laravel auditing.&lt;/p&gt;

&lt;p&gt;To configure, edit the config/app.php and add the following to providers' array.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'providers' =&amp;gt; [
    // ...

    OwenIt\Auditing\AuditingServiceProvider::class,

    // ...
],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Run the following commands to configure and to create audit tables:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="config"
php artisan vendor:publish --provider "OwenIt\Auditing\AuditingServiceProvider" --tag="migrations"
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now, let us configure the models to start tracking the audit trails.&lt;/p&gt;

&lt;p&gt;Edit Post.php and Comment.php to add laravel auditing traits and interfaces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;OwenIt\Auditing\Contracts\Auditable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Auditable&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;\OwenIt\Auditing\Auditable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once this is done, whenever you create a Post using this model, it will create a corresponding record in the &lt;em&gt;audit&lt;/em&gt; table. To retrieve the audit trail of a particular post, you can use the auto injected relationships of Posts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'audits'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;//Get the first post&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above will give you the user id of who created the post. There could be a challenge if the application is not using the default user table nor the Auth middleware. In this case, Audit will not catch the user id. Instead it will pass in a null value for user id. &lt;/p&gt;

&lt;p&gt;Nevertheless, we have a fix for that.&lt;/p&gt;

&lt;p&gt;In app folder, create another folder- "Resolvers" and a file inside that- "UserResolver.php".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Resolvers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Support\Facades\Auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Support\Facades\Config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;App\User&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserResolver&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;\OwenIt\Auditing\Contracts\UserResolver&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * {@inheritdoc}
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$guards&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'audit.user.guards'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'web'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'api'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$guards&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$guard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$guard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;check&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;  &lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$guard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'userid'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, tell Laravel Audit to resolve users using this file. For that, edit config/audit.php to have the newly created user resolved class configurations in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'resolver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'user'&lt;/span&gt;       &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;App\Resolvers\UserResolver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'ip_address'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;OwenIt\Auditing\Resolvers\IpAddressResolver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'user_agent'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;OwenIt\Auditing\Resolvers\UserAgentResolver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'url'&lt;/span&gt;        &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;OwenIt\Auditing\Resolvers\UrlResolver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And thats it! Auditing will work like a gem!&lt;/p&gt;

&lt;p&gt;As always, say thanks to the package maintainers: &lt;a href="http://laravel-auditing.com/"&gt;http://laravel-auditing.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to use Passport with Laravel Modules (nwidart)</title>
      <dc:creator>Santhosh</dc:creator>
      <pubDate>Wed, 18 Sep 2019 03:18:58 +0000</pubDate>
      <link>https://dev.to/santhoshj/how-to-use-passport-with-laravel-modules-nwidart-4hhi</link>
      <guid>https://dev.to/santhoshj/how-to-use-passport-with-laravel-modules-nwidart-4hhi</guid>
      <description>&lt;p&gt;Let me introduce these two packages for those who are new to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Laravel Passport:&lt;/strong&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the &lt;a href="https://github.com/thephpleague/oauth2-server"&gt;League OAuth2 server&lt;/a&gt; that is maintained by Andy Millington and Simon Hamp.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note: I followed the Medium article to configure Passport - &lt;a href="https://medium.com/techcompose/create-rest-api-in-laravel-with-authentication-using-passport-133a1678a876"&gt;Here is the medium article for further reference.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Laravel Modules:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;nwidart/laravel-modules&lt;/code&gt;  is a Laravel package which was created to manage your large Laravel app using modules. A module is like a Laravel package, it has some views, controllers or models. This package is supported and tested in Laravel 5.&lt;/p&gt;

&lt;p&gt;This package is a re-published, re-organised and maintained version of &lt;a href="https://github.com/pingpong-labs/modules"&gt;pingpong/modules&lt;/a&gt;, which isn't maintained anymore. This package is used in  &lt;a href="https://asgardcms.com/"&gt;AsgardCMS&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When creating enterprise applications, it will be much useful to develop them as modular as possible to make it maintainable. I was going through a scenario where I have to implement a users module using Laravel Module and authentication is using Passport. Here is how I did it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Start a new laravel project&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project laravel/laravel my-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For making it less clutter, I moved the default migrations out of this project. We will later be adding it back to the project under user module.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2. Install and configure Laravel Modules:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require nwidart/laravel-modules
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3. Add Modules folder to autoload array in composer.json:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modules\\": "Modules/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;4. Run auto load&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer dumpautoload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;5. Create Users module and model:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan module:make Users
php artisan module:make-model User Users
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;6. Copy the migration under *my-app/Modules/Users/Database/Migrations&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;We will set up the login and registration functionalities once we finish configuring the Passport.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Install and configure Passport:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require laravel/passport
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;8. Edit config/app.php to add Passport service provider&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'providers' =&amp;gt;[
Laravel\Passport\PassportServiceProvider::class,
],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;9. Run migrate and install - Install will create the token keys for security.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate
php artisan passport:install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;10. Move the app/User.php to Modules/Users/Entities/User.php&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. app/Providers/AuthServiceProvider.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace App\Providers;
use Laravel\Passport\Passport; 
use Illuminate\Support\Facades\Gate; 
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider 
{ 
    /** 
     * The policy mappings for the application. 
     * 
     * @var array 
     */ 
    protected $policies = [ 
        'App\Model' =&amp;gt; 'App\Policies\ModelPolicy', 
    ];
/** 
     * Register any authentication / authorization services. 
     * 
     * @return void 
     */ 
    public function boot() 
    { 
        $this-&amp;gt;registerPolicies(); 
        Passport::routes(); 
    } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;12. IMPORTANT&lt;/strong&gt;: In config/auth.php, specify the api authentication driver to passport. Also, in providers, let laravel know that we have to use the User class inside Modules to be the model. &lt;strong&gt;NOT&lt;/strong&gt; the default app/User::class.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
return ['guards' =&amp;gt; [   
    'web' =&amp;gt; [   
        'driver' =&amp;gt; 'session',   
        'provider' =&amp;gt; 'users',   
    ],   
    'api' =&amp;gt; [   
        'driver' =&amp;gt; 'passport',   
        'provider' =&amp;gt; 'users',   
    ],   
],
.
.
.
'providers' =&amp;gt; [
        'users' =&amp;gt; [
            'driver' =&amp;gt; 'eloquent',
            'model' =&amp;gt; Modules\Users\Entities\User::class,
        ],

        // 'users' =&amp;gt; [
        //     'driver' =&amp;gt; 'database',
        //     'table' =&amp;gt; 'users',
        // ],
    ],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;13. Add API Routes in Modules/Users/Routes/api.php&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::post('login', 'UsersController@login');
Route::post('register', 'UsersController@register');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;14. Create Users Controller - Laravel Module will create this on its own when we create the model.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace Modules\Users\Http\Controllers;

use Validator;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Users\Entities\User;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth; 

class UsersController extends Controller
{
    public $successStatus = 200;

    /**
    * Handles user logins
    *
    * @return void
    */
    public function login()
    {
        if(Auth::attempt(['email' =&amp;gt; request('email'), 'password' =&amp;gt; request('password')])){ 
            $user = Auth::user(); 
            $success['token'] =  $user-&amp;gt;createToken('my-app')-&amp;gt; accessToken; 
            return response()-&amp;gt;json(['success' =&amp;gt; $success], $this-&amp;gt; successStatus); 
        } 
        else{ 
            return response()-&amp;gt;json(['error'=&amp;gt;'Unauthorised'], 401); 
        } 
    }

    /** 
     * Register api 
     * 
     * @return \Illuminate\Http\Response 
     */ 
    public function register(Request $request) 
    { 
        $validator = Validator::make($request-&amp;gt;all(), [ 
                'name' =&amp;gt; 'required', 
                'email' =&amp;gt; 'required|email', 
                'password' =&amp;gt; 'required', 
                'c_password' =&amp;gt; 'required|same:password', 
            ]);
            if ($validator-&amp;gt;fails()) { 
                return response()-&amp;gt;json(['error'=&amp;gt;$validator-&amp;gt;errors()], 401);            
            }
            $input = $request-&amp;gt;all(); 
            $input['password'] = bcrypt($input['password']); 
            $user = User::create($input); 
            $success['token'] =  $user-&amp;gt;createToken('MyApp')-&amp;gt; accessToken; 
            $success['name'] =  $user-&amp;gt;name;
            return response()-&amp;gt;json(['success'=&amp;gt;$success], $this-&amp;gt; successStatus); 
        }

        /** 
        * details api 
        * 
        * @return \Illuminate\Http\Response 
        */ 
        public function details() 
        { 
            $user = Auth::user(); 
            return response()-&amp;gt;json(['success' =&amp;gt; $user], $this-&amp;gt; successStatus); 
        } 

    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;15.&lt;/strong&gt; In command line, run the server and you will be able to test it using Postman or Insomnia.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan serve
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Please feel free to ask any questions on this article. I will be happy to help. Reach me - santhoshj at gmail dot com.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Written with &lt;a href="https://stackedit.io/"&gt;StackEdit&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Laravel 6 and Make Auth</title>
      <dc:creator>Santhosh</dc:creator>
      <pubDate>Sat, 07 Sep 2019 08:43:03 +0000</pubDate>
      <link>https://dev.to/santhoshj/laravel-6-and-make-auth-4aln</link>
      <guid>https://dev.to/santhoshj/laravel-6-and-make-auth-4aln</guid>
      <description>&lt;p&gt;&lt;strong&gt;Laravel 6&lt;/strong&gt; came with a hero's welcome this Tuesday. When I started to try to see how the new one is, I was straight away hit at a wall. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:auth
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Make Auth command is not anymore available in Laravel 6. But, don't worry. we are covered with a better solution for this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;New package - Laravel UI&lt;/strong&gt;&lt;br&gt;
Laravel has come up with a new package - Laravel UI which separates UI part of an application apart. This could help developers to create a server only applicaiton (API host) or a Console only application, without having the extra burden of UI attached to it.&lt;/p&gt;

&lt;p&gt;Laravel UI can be installed with composer command:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project laravel/laravel blog
cd blog
composer require laravel/ui
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once this package is installed successfully, it gives a few new artisan commands.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;➜ php artisan ui --help
Description:
  Swap the front-end scaffolding for the application

Usage:
  ui [options] [--] &amp;lt;type&amp;gt;

Arguments:
  type                   The preset type (bootstrap, vue, react)

Options:
      --auth             Install authentication UI scaffolding
      --option[=OPTION]  Pass an option to the preset command (multiple values allowed)
  -h, --help             Display this help message
  -q, --quiet            Do not output any message
  -V, --version          Display this application version
      --ansi             Force ANSI output
      --no-ansi          Disable ANSI output
  -n, --no-interaction   Do not ask any interactive question
      --env[=ENV]        The environment the command should run under
  -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;UI can be initiated with either VueJS or with React.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan ui vue 
php artisan ui react
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For creating authentication scaffolds, add --auth to the ui command. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan ui:auth
php artisan ui:auth --views
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Running the first command sets up the routes and scaffolds for authentication and second one sets up the views.&lt;/p&gt;

&lt;p&gt;Fun time's up! Let us get back to work...!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>tutorial</category>
      <category>php</category>
      <category>help</category>
    </item>
  </channel>
</rss>
