# PHP Laravel Framework

# Laravel Force Https

```php
use Illuminate\Routing\UrlGenerator;

public function boot(UrlGenerator $url)
{
    public function register()
    {
      // force https
      if (config('app.env') !== 'local' && !filter_var(request()->getHost(), FILTER_VALIDATE_IP)) {
          $this->app['request']->server->set('HTTPS', true);
    }
}
```

need to put into AppServiceProvider

\# new version

```php  
public function register(): void  
{  
 $this-&gt;app\['request'\]-&gt;server-&gt;set('HTTPS', true);  
}

public function boot(): void  
{  
 if (strtolower(config('app.env')) == 'production') {  
 \\URL::forceScheme('https');  
 }  
}  
```  
   
\# old version

```php  
use Illuminate\\Routing\\UrlGenerator;

public function boot(UrlGenerator $url)  
{  
 if (config('app.env') !== 'local' &amp;&amp; !filter\_var(request()-&gt;getHost(), FILTER\_VALIDATE\_IP)) {  
 $this-&gt;app\['request'\]-&gt;server-&gt;set('HTTPS', true);  
 }  
}

public function register()  
{  
 if (config('app.env') !== 'local' &amp;&amp; !filter\_var(request()-&gt;getHost(), FILTER\_VALIDATE\_IP)) {  
 $this-&gt;app\['request'\]-&gt;server-&gt;set('HTTPS', true);  
 }  
}  
```

# basic guide with git

1\) Copy .env.example to .env:

```shell
cp .env.example .env
```

ATAU

Copy-Item .env.example -Destination .env

  
Kalau env.example takdak, manual ja buat file pastu copy content dari project lain

2\) Install dependencies:

composer install

\*\*In case kalau problem gak, run composer du

3\) Generate application key:

php artisan key:generate

  
4\) Set up database (if required):

\- Update .env with your database credentials.  
   
\- Run migrations (if needed):  
   
 php artisan migrate

  
5\) Install Node.js dependencies (if using Vite or frontend assets):

npm install

  
6\) Build frontend assets (if applicable):

npm run build # or npm run dev for development (has hot refresh)

  
8\) Start the Laravel development server (if needed):

php artisan serve

  
9\) (Optional) Run queue workers if using queues:

php artisan queue:work

\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Everytime modify the web.php or routing, run this command

art optimize

# laravel cron setting

```sh  
vi /etc/crontab  
```

jangan guna crontab -e, cron tak jalan betul nanti.

```sh  
\* \* \* \* \* root cd /var/www/html/&lt;namaprojek&gt; &amp;&amp; php artisan schedule:run &gt;&gt; /dev/null 2&gt;&amp;1  
```

# session expired

1\. check ``SESSION\_SECURE\_COOKIE`` setting in ``.env`` or ``config/session``

if its true, then ssl is needed. if open webapp using ip without https. make it false

2\. force https in ``AppServiceProvider``

if its true, then ssl is needed.

3\. low ``output\_buffering`` in php.ini

\* open php.ini that been used in webserver  
\* increase output\_buffering eg. ``output\_buffering = 20000``

note: output\_buffering \[ob\_\](https://www.php.net/manual/en/function.ob-start.php) is used by php to cache the html that been render before submit to user. to increase client site performance.

# Spatie Media file '/' does not exist

&gt; Spatie \\ MediaLibrary \\ Exceptions \\ FileCannotBeAdded \\ FileDoesNotExist  
&gt; File '/' does not exist

\- check medialibrary config  
```  
'max\_file\_size' =&gt; 1024 \* 1024 \* 10 // For 10MB   
```

\- check nginx.conf max  
```  
client\_max\_body\_size 2M;  
```

\- check php.ini  
```  
upload\_max\_filesize = 5M  
```