Entries tagged laravel

Default Filter in Laravel Nova

Posted on 20. Februar 2023 Comments

There is currently no way to use a Filter for a Resource in the default view. However, for the desired outcome, it’s possible to use a combination of the Nova indexQuery method and Laravels global scopes.

  1. Create a scope with the desired default filter: php artisan make:scope DefaultFilterScope
  2. Use this scope in the indexQuery method of the Nova Resource like so:
    public static function indexQuery(NovaRequest $request, $query) { return $query->withGlobalScope(DefaultFilterScope::class, new DefaultFilterScope); }
  3. In the other Filters for this Resource, add $query = $query->withoutGlobalScope(DefaultFilterScope::class); in the first line of the apply method, to remove the Global Scope added in the indexQuery method, and just use whatever the Filter does.

Source

404 with content using basic auth with nginx

Posted on 19. Juli 2022 Comments

When using e.g. Laravel Forge you can create a section of the website that’s restricted and uses HTTP Basic Access Authentication for access control. When the credentials aren’t entered correctly, the server returns a 401 error.

Basic Authentication prompt

However, when you would like to restrict a section that’s part of an application, whatever rules you defined in your /etc/nginx/sites-available/domain.tld.conf you have to now add to the new location section.

Example

On a staging server, you want to restrict access to /register and /login. Head to servers/X/sites/Y/security URL by clicking on the server, then select a site and click on „Security“. These entries create 2 files in /etc/nginx/forge-conf/domain.tld/server where ID is the ID you can see in Forge..

  • .htpasswd-ID
  • protected_site-ID.conf
Security Rules in Laravel Forge

However, when you now navigate to e.g. /login and enter your credentials, you will see a 404 for /login in the debug console. A request has been made and returned the content – just with the wrong status code. This is happening because there is no file called „login“ in the webserver public folder and nginx hasn’t been instructed to use PHP for this particular location.

tl;dr: You need to add this line to the location entry in the .conf files:

try_files $uri $uri/ /index.php?$query_string;

The whole file now looks like this:

location /register {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/forge-conf/domain.tld/server/.htpasswd-42;
try_files $uri $uri/ /index.php?$query_string;
}

Deploy with envoyer and artisan

Posted on 20. Mai 2020 Comments

I just revived JustParks Envoyer Deploy package and updated it for Laravel 5.5+ (handle() and fire() – both work now), 6 and 7. I haven’t written it, so all the credit goes to Dayle Rees/JustPark.

Updated envoyer:deploy package on packagist

As you can tell from the README, Install like so:

composer require repat/envoyer-deploy --dev

Then publish the config file by executing this and selecting the number that says JustPark\Deploy\ServiceProviders\EnvoyerServiceProvider.

php artisan vendor:publish

In the envoyer.php config file, fill in the unique ID that comes after the /deploy in the link you can find in the Deployment Hooks tab in envoyer, e.g. https://envoyer.io/deploy/4aLDdfsfsd4s6fSzeKGNfakekey75R45wOwTQULEDJNrj

You can now deploy with

php artisan envoyer:deploy

Sourcecode for repat/envoyer-deploy on GitHub

Laravel QueueWorker: database not configured

Posted on 4. November 2019 Comments

It doesn’t matter which queue system you’re using: Redis, a relational database such as MySQL or a cloud solution like AWS SQS ; you can always store failed jobs in a normal database table. This configuration is done in config/queue.php in the last array:

'failed' => [
    'driver' => 'database',
    'database' => 'default', // NOTE: connection, not database!
    'table' => 'jobs_failed',
],

The key is to put in the connection name, not the database name for ‚database‘. If you don’t you will get the error message „Database default not configured“ every time a job fails instead.

Check out my Laravel Job Eloquent Models.


Laravel: Serialization of ‚Closure‘ is not allowed with Jobs and Queues

Posted on 26. Februar 2018 Comments

When dispatching Jobs onto a queue it’s possible to get the following error:

Serialization of ‚Closure‘ is not allowed

This basically means that you are either passing a class that contains anonymous functions („closures“ in PHP) or that in the Job itself there is an anonymous function. This could also be a library you are using. PHP is unable to serialize closures per se. When Jobs get dispatched onto the queue, everything has to be serialized before. If you absolutely must use them this way, look into the Super Closure package by jeremeamia. However, just rewriting the code a little might be easier:

 

Solution

Instead of passing a whole object to the Job like so:

$objOfClassWithClosures = ...;
$job = new Job($objOfClassWithClosures);
dispatch($job);

Pass the Fully Qualified Class Name (that’s what ClassName::class returns) to the job and instantiate the object on the other side, possibly even using Laravels Container:

$job = new Job(ClassWithClosures::class);
dispatch($job);

Inside the job you can then use the constructor to make a new instance of that class:

public function __construct(string $fqcn) {
$objOfClassWithClosures = app()->make($fqcn);
}

 

PHP + Laravel development Packages for atom.io

Posted on 30. Januar 2018 Comments

PHP CS Fixer

brew install php-cs-fixer

apm install php-cs-fixer

Settings:

php path: /usr/local/bin/php

php-cs-fixer path: /usr/local/bin/php-cs-fixer

PHP CS Fixer Rules: @PSR2,blank_line_after_opening_tag,whitespace_after_comma_in_array,blank_line_after_namespace

PHP CS Fixer Arguments: --using-cache=no, --no-interaction, --stop-on-violation

[x] Show Notifications

Ctrl+Cmd+S

Can also be used on commandline for folders, e.g.
$ php-cs-fixer fix app/ --rules=@PSR2,blank_line_after_opening_tag,whitespace_after_comma_in_array,blank_line_after_namespace

 

php-integrator-refactoring (php-integrator-base)

apm install php-integrator-base

apm install php-integrator-refactoring

Linting (also see linter(-php))

Sort use statements

 

linter and linter-php

apm install linter

apm install linter-php

In case linting with php-integrator-base does not work.

 

language-blade and language-vue

apm install language-blade

apm install language-vue

 

minimap

apm install minimap

Short overview over the code on the right side

 

highlight-selected

apm install highlight-selected

 

platform-ide-terminal

apm install platform-ide-terminal

Instead of switching windows, terminal right in atom

Cmd+Shift+T

 

symbols-tree-view

apm install symbols-tree-view

Structured overview on the right side over classes, constants, attributes, methods etc

 

hyperclick and hyperclick-php

apm install hyperclick

apm install hyperclick-php

Cmd+Click on classes, functions etc to go to origin.

Not needed if you installed atom-ide-ui

 

teletype

apm install teletype

Work on the same file with coworkers, encrypted via WebRTC

 

More dependencies might be installed with installing some of these packages

PHP Commandline Notification

Posted on 3. Januar 2018 Comments

When you run long (PHP) jobs it’s easy to forget about the terminal. That’s why I’ve created a small PHP package that will make a little sound. Because PHP itself doesn’t have this functionality anymore it’s possible to just echo the ASCII sign for BEL. Simply put this at the end of your PHP job:

use repat\CommandlineBell;
// flashes screen if possible, otherwise just bell()
CommandlineBell::flash();
// makes a beep sound
CommandlineBell::bell();

Under the hood it’s really just:
echo "0x07";

A good (non-PHP) alternative I found is brb by Viktor Fröberg, just run commands like this

php artisan migrate --seed ; brb

Of course it’s always possible to just run a TTS app on the commandline via exec() like this:

exec("say terminal task done");

Or you could do it similar to brb:

php artisan migrate --seed ; say seeding done

Source on GitHub