PHP Laravel Framework


Laravel Force Https

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->app['request']->server->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' && !filter_var(request()->getHost(), FILTER_VALIDATE_IP)) {
                $this->app['request']->server->set('HTTPS', true);
        }
}

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

basic guide with git

1) Copy .env.example to .env:

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/<namaprojek> && php artisan schedule:run >> /dev/null 2>&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

> Spatie \ MediaLibrary \ Exceptions \ FileCannotBeAdded \ FileDoesNotExist
> File '/' does not exist

- check medialibrary config
```
'max_file_size' => 1024 * 1024 * 10 // For 10MB 
```

- check nginx.conf max
```
client_max_body_size 2M;
```

- check php.ini
```
upload_max_filesize = 5M
```