<?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>Forem: faisal ahmed</title>
    <description>The latest articles on Forem by faisal ahmed (@thephpx).</description>
    <link>https://forem.com/thephpx</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%2F255877%2F57dba353-7c6f-4274-b7f8-965009609ae0.png</url>
      <title>Forem: faisal ahmed</title>
      <link>https://forem.com/thephpx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thephpx"/>
    <language>en</language>
    <item>
      <title>Laravel 11 : Custom package does not generate CSRF token.</title>
      <dc:creator>faisal ahmed</dc:creator>
      <pubDate>Wed, 08 May 2024 13:31:02 +0000</pubDate>
      <link>https://forem.com/thephpx/laravel-11-custom-package-does-not-generate-csrf-token-4oi0</link>
      <guid>https://forem.com/thephpx/laravel-11-custom-package-does-not-generate-csrf-token-4oi0</guid>
      <description>&lt;p&gt;I encountered this problem while trying to generate csrf token in form on a laravel 11 based custom package. &lt;/p&gt;

&lt;p&gt;The routes in my custom package already included the default web middleware but still it was not generating the needed csrf token for routes and views which belonged to the custom laravel package.&lt;/p&gt;

&lt;p&gt;The solution was to include &lt;code&gt;StartSession&lt;/code&gt; and &lt;code&gt;VerifyCsrfToken&lt;/code&gt; middleware manually inside the &lt;code&gt;app.php&lt;/code&gt; file to be used as a global middleware.&lt;br&gt;
&lt;/p&gt;

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

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    -&amp;gt;withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    -&amp;gt;withMiddleware(function (Middleware $middleware) {
        $middleware-&amp;gt;append(\Illuminate\Session\Middleware\StartSession::class);
        $middleware-&amp;gt;append(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class);
    })
    -&amp;gt;withExceptions(function (Exceptions $exceptions) {
        //
    })-&amp;gt;create();

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

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>csrf</category>
      <category>package</category>
    </item>
    <item>
      <title>Loading custom laravel packages locally</title>
      <dc:creator>faisal ahmed</dc:creator>
      <pubDate>Mon, 28 Aug 2023 14:06:38 +0000</pubDate>
      <link>https://forem.com/thephpx/loading-custom-laravel-packages-locally-13la</link>
      <guid>https://forem.com/thephpx/loading-custom-laravel-packages-locally-13la</guid>
      <description>&lt;p&gt;I normally store my locally developed packages inside the &lt;code&gt;packages&lt;/code&gt; folder in my laravel projects root. &lt;/p&gt;

&lt;p&gt;To ensure your core laravel application recognizes these customer packages we have to update our main &lt;code&gt;composer.json&lt;/code&gt; file and add the following line :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    "repositories": [
        {
          "type": "path",
          "url": "packages/thephpx/*"
        }
    ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice I have used &lt;code&gt;*&lt;/code&gt; in the package url, that is because i want composer to load all available packages inside the packages folder with the given namespace.&lt;/p&gt;

&lt;h1&gt;
  
  
  Custom Packages:
&lt;/h1&gt;

&lt;p&gt;Before creating any new custom package I create my namespace folder first inside the packages folder. In my case it's &lt;code&gt;thephpx&lt;/code&gt; folder. &lt;/p&gt;

&lt;p&gt;Then i create the package folder inside the namespace folder. For example if I am creating a demo package, I will create a &lt;code&gt;demo&lt;/code&gt; folder inside the &lt;code&gt;thephpx&lt;/code&gt; folder and then create a &lt;code&gt;composer.json&lt;/code&gt; file inside the demo folder. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;composer.json&lt;/code&gt; file content looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "guestguide/demo",
    "type": "library",
    "description": "Demo package from thephpx",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Guestguide\\Demo\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Faisal Ahmed",
            "email": "thephpx@gmail.com"
        }
    ],
    "extra": {
        "laravel": {
            "providers": [                "Thephpx\\Demo\\DemoServiceProvider"
            ]
        }
    },
    "require-dev": {
        "orchestra/testbench": "^8.0"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the &lt;code&gt;composer.json&lt;/code&gt; file is created, then I create the &lt;code&gt;src&lt;/code&gt; folder and inside the &lt;code&gt;src&lt;/code&gt; folder create &lt;code&gt;Http\Controllers&lt;/code&gt; folder. In a custom package the &lt;code&gt;src&lt;/code&gt; folder can have same folders as there are in the core laravel applications &lt;code&gt;app&lt;/code&gt; folder. &lt;/p&gt;

&lt;p&gt;Also inside the &lt;code&gt;src&lt;/code&gt; folder I create the &lt;code&gt;DemoServiceProvider.php&lt;/code&gt; class. Which is the custom service provider class for this custom package.&lt;/p&gt;

&lt;p&gt;The content of the &lt;code&gt;DemoServiceProvider.php&lt;/code&gt; class is as follows:&lt;br&gt;
&lt;/p&gt;

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

namespace Thephpx\Dashboard;

use Illuminate\Support\ServiceProvider;

class DemoServiceProvider extends ServiceProvider
{
  public function register()
  {
    //
  }

  public function boot()
  {
    //
    $this-&amp;gt;loadRoutesFrom(__DIR__.'/../routes/web.php');

    $this-&amp;gt;loadViewsFrom(__DIR__.'/../resources/views', 'dashboard');

    if ($this-&amp;gt;app-&amp;gt;runningInConsole()) {
      $this-&amp;gt;publishes([
        __DIR__.'/../resources/assets' =&amp;gt; public_path('dashboard'),
      ],'assets');
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, I go on to create the other related folders such as &lt;code&gt;\resources\views&lt;/code&gt;, &lt;code&gt;\resources\assets&lt;/code&gt;, &lt;code&gt;routes&lt;/code&gt; etc. &lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;routes&lt;/code&gt; folder I then create &lt;code&gt;web.php&lt;/code&gt; route file. The content of the file is as follows:&lt;br&gt;
&lt;/p&gt;

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

use Illuminate\Support\Facades\Route;

Route::group(['middleware'=&amp;gt;'web'], function () {
    Route::get('/demo', \Thephpx\Demo\Http\Controllers\DemoController::class)-&amp;gt;middleware('auth');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: here in the web.php I have used &lt;code&gt;web&lt;/code&gt; middleware which never had to be mentioned explicitly in the core laravel &lt;code&gt;web.php&lt;/code&gt; route file. But, in order for your package to get session data you need to include it in your package route file. Otherwise &lt;code&gt;auth()&lt;/code&gt; helper or other Auth related methods does not work as expected.&lt;/p&gt;

&lt;p&gt;Once everything is in place run &lt;code&gt;composer install&lt;/code&gt; inside the package folder where you have created it's own &lt;code&gt;composer.json&lt;/code&gt; file and install the required testbench library.  Now you are ready to start developing your custom package.&lt;/p&gt;

&lt;p&gt;This is suppose to be a basic startup document for further in-depth details you can visit &lt;a href="https://www.laravelpackage.com/"&gt;https://www.laravelpackage.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>package</category>
      <category>composer</category>
    </item>
    <item>
      <title>Auto translate page based on subdomain in a Laravel application</title>
      <dc:creator>faisal ahmed</dc:creator>
      <pubDate>Mon, 21 Aug 2023 13:08:15 +0000</pubDate>
      <link>https://forem.com/thephpx/auto-translate-page-based-on-subdomain-in-a-laravel-application-484g</link>
      <guid>https://forem.com/thephpx/auto-translate-page-based-on-subdomain-in-a-laravel-application-484g</guid>
      <description>&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;&lt;br&gt;
I have a web application which is mainly built in english but the client required a functionality where the the website could be loaded in multiple subdomains based on country. For example; the main website is at &lt;a href="http://www.domain.com"&gt;www.domain.com&lt;/a&gt; which will show content in english but if the website is accessed in bd.domain.com then the same english content needed to be translated seamlessly using google translate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
I have already created a writeup where I toggled laravel locale based on subdomain by identifying the subdomain in AppServiceProvider. As an extension to that work I also introduced two variables that got passed to all available views using &lt;code&gt;view::share()&lt;/code&gt; method. The resulting code looks something like this -&lt;br&gt;
&lt;/p&gt;

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

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        if(!empty($_SERVER['HTTP_HOST'])){
            //subdomain specific locale
            $subdomain = current(explode('.', $_SERVER['HTTP_HOST']));

            if (in_array($subdomain, ["www","in"])) {
                if($subdomain !== 'www'){
                    app()-&amp;gt;setLocale($subdomain);
                    config('app.url',$subdomain.'.domain.com');
                    View::share('translate',true);
                    if($subdomain == 'in') View::share('translate_lang','hi');
                    if($subdomain == 'bd') View::share('translate_lang','bn');                    
                } else {
                    app()-&amp;gt;setLocale('en');
                    config('app.url','www.domain.com');
                    View::share('translate',false);
                    View::share('translate_lang','en');
                }            
            } else {
                app()-&amp;gt;setLocale('en');
                config('app.url','www.domain.com');
            }
        }
    }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This meant now I had two variable available which I could check in the blade templates to load the google translate JavaScript code block and initialize the translation on page load. The blade layout file looked as follows -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   &amp;lt;html&amp;gt;
    &amp;lt;head&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
      @if($data['translate'] == true)&amp;lt;div id="google_translate_element" style="display:none;"&amp;gt;&amp;lt;/div&amp;gt;@endif
      &amp;lt;div class="container"&amp;gt;
        &amp;lt;p&amp;gt;Your content goes here.&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
      @if(view()-&amp;gt;shared('translate') == true)
      &amp;lt;script type="text/javascript"&amp;gt;
      function setCookie(key, value, expiry) {
          var expires = new Date();
          expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
          document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
      }

      function googleTranslateElementInit() {
          setCookie('googtrans', '/en/{{view()-&amp;gt;shared("translate_lang")}}',1);
          new google.translate.TranslateElement({
              pageLanguage: 'en'
          }, 'google_translate_element');
      }
      &amp;lt;/script&amp;gt;
      &amp;lt;script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"&amp;gt;
      &amp;lt;/script&amp;gt;
      @endif
    &amp;lt;/body&amp;gt;
  &amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, now when the website is accessed via &lt;code&gt;www.domain.com&lt;/code&gt; it loads the regular english language content but if the same page is visited via &lt;code&gt;bd.domain.com&lt;/code&gt; then the same english content is translated to bengali language on page load. Further more; if there are certain content that needs to be loaded using local language file that also works as laravel sets the local to appropriate language based on subdomain.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>translate</category>
      <category>locale</category>
      <category>language</category>
    </item>
    <item>
      <title>Laravel subdomain specific locale</title>
      <dc:creator>faisal ahmed</dc:creator>
      <pubDate>Mon, 21 Aug 2023 13:03:10 +0000</pubDate>
      <link>https://forem.com/thephpx/laravel-subdomain-specific-locale-fb</link>
      <guid>https://forem.com/thephpx/laravel-subdomain-specific-locale-fb</guid>
      <description>&lt;p&gt;I needed to change the locale of one of my laravel based projects based on the subdomain. The setup was as follows -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.domain.com"&gt;www.domain.com&lt;/a&gt; (en)&lt;/li&gt;
&lt;li&gt;de.domain.com (de)&lt;/li&gt;
&lt;li&gt;es.domain.com (es)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I implemented the following inside AppServiceProvider to get the job done:&lt;br&gt;
&lt;/p&gt;

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

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //subdomain specific locale
        $subdomain = current(explode('.', $_SERVER['HTTP_HOST']));

        if (in_array($subdomain, ["www","fr","de"])) {
            if($subdomain !== 'www'){
                app()-&amp;gt;setLocale($subdomain);
                config('app.url',$subdomain.'.domain.com');
            } else {
                app()-&amp;gt;setLocale('en');
                config('app.url','www.domain.com');
            }            
        } else {
            app()-&amp;gt;setLocale('en');
            config('app.url','www.domain.com');
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this helps someone in future!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>language</category>
      <category>locale</category>
    </item>
  </channel>
</rss>
