Zini

Experience

675

0 Best Reply Awards

  • Member Since 3 Years Ago
  • 0 Lessons Completed
  • 0 Favorites

12th July, 2018

Zini left a reply on Fake A File Upload With Defined Mimetype • 1 week ago

Thank you lostdreamer_nl, you are correct, it actually works to that point. I don't know how I missed that, I was looking in the wrong place.

Actually my problem happens after the upload, when I control the input file in a custom rule.

The uploaded file seems ok:

\Log::debug(print_r($file, true));

testing.DEBUG: Illuminate\Http\Testing\File Object
(
    [name] => esse.pdf
    [tempFile] => Resource id #645
    [sizeToReport] => 0
    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => esse.pdf
    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/pdf
    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
    [hashName:protected] => 
    [mimeType] => application/pdf
    [pathName:SplFileInfo:private] => /tmp/phpFe5R0e
    [fileName:SplFileInfo:private] => phpFe5R0e
)

But when I try to get its mimetype, it returns an unrelated value:

$mimetype = \File::mimeType($file);
\Log::debug($mimetype);
// testing.DEBUG: filemimetyperule = inode/x-empty

In the case of a JPG image I even get png/image.

Is it wrong to use \File::mimeType() to control the an uploaded file?

11th July, 2018

Zini started a new conversation Fake A File Upload With Defined Mimetype • 1 week ago

Hi,

I want to fake the upload of multiple types. It works for images, but I have a hard time finding out how to simulate other types of files.

Any not-image file gets inode/x-empty for mimetype, where I would like to see application/pdf.

// Mimetype ok
$file = \Illuminate\Http\UploadedFile::fake()->image('test.jpg');

// Mimetype issue: inode/x-empty
$file = \Illuminate\Http\UploadedFile::fake()->create('test.pdf');

// also tried this
$file->mimeType = 'application/pdf';

I did not find yet any solution in the Illuminate doc, or the Symfony doc.

21st June, 2018

Zini left a reply on Custom Validation Class: Parameters Support • 4 weeks ago

It is actually pretty straightforward using the constructor:

// parameter for validation rule extending a service provider
$request->validate([
    'field' => ['foo:parameter'],
]);

// parameter for custom class rule
$request->validate([
    'field' => [new foo($parameter)],
]);

@bobbybouwmann I don't know why but I was looking for some custom Laravel way to do that...

20th June, 2018

Zini left a reply on Phpunit File Upload: Size 0 (zero) • 4 weeks ago

@rin4ik ok, I tried using the call() method as explained the stack answer, but it does not impact the result: thhe file's size remains 0.

Zini started a new conversation Custom Validation Class: Parameters Support • 4 weeks ago

Laravel 5.5 introduced custom validation classes: https://laravel.com/docs/5.6/validation#using-rule-objects

Extension rules allow input parameters:

    Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
        return $value == 'foo';
    });

How do we achieve the same support of parameters with a validation class?

Zini left a reply on Phpunit File Upload: Size 0 (zero) • 4 weeks ago

@rin4ik I already found this post, but it dates back to 2015 when - I suppose - there was no UploadedFile::fake().

I thought that there is now another approach.

Zini started a new conversation Phpunit File Upload Fails • 4 weeks ago

My test makes a post request:

// FileUploadTest.php

$this->post(
    'uri',
    ['file' => \Illuminate\Http\UploadedFile::fake()->image('file.jpg')->size(200)]
);

The controller detects the filesize:

// FileUploadController.php

if ($request->hasFile('file')) {
    \Log::debug('bytes: ' . \File::size($request->file));
}

When I upload a file manually, the correct size of the file would be logged. When I run a phpunit test, the size is always 0.

Any idea what is going on?

20th March, 2018

Zini left a reply on Validate A URL Parameter With Model Binding • 3 months ago

@ahmeddabak good guess, looking at my routes I think you are right, it is what is happening. However it isn't the behaviour I would expect.

@Cronix I agree, the issue with with() is probably related to Dingo package.

But, is there a way in Laravel to validate URL parametersusing custom validation rules instead of the above simple regular expressions?

Zini left a reply on Validate A URL Parameter With Model Binding • 3 months ago

I send the same request each time. It works fine until I add the where() clause, at which point I get this error.

Zini left a reply on Validate A URL Parameter With Model Binding • 3 months ago

@ahmeddabak thanks, but I use dingo/api and it doesn't like the where():

"message": "405 Method Not Allowed",
"status_code": 405,
"debug": {
    "line": 255,
    "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
    "class": "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"
}

Besides, isn't there any way to use validation rules on url parameters instead of simple regular expression constraints?

Zini started a new conversation Validate A URL Parameter With Model Binding • 3 months ago

My route parameter:

$api->get('projects/{project}', 'App\Http\Controllers\[email protected]');

has a model binding:

public function show(Project $project) { }

The projectparameter is an uuid, and the SQL query crashes when a wrong format is given. Therefore how can I check this this parameter with my custom validation rule before the binding is applied?

    Validator::extend('uuid', function ($attribute, $value, $parameters, $validator) {
        return preg_match('/[a-f0-9]{8}\-[a-f0-9]{4}\-4[a-f0-9]{3}\-(8|9|a|b)[a-f0-9]{3‌​}\-[a-f0-9]{12}/', $value);
    });

30th January, 2018

Zini left a reply on Forge And Digital Ocean - Meltdown And Spectre Vulnerabilities • 5 months ago

Of course you should update your servers. Forge is not a server, it is a tool pretty much limited to deploying your code on a server.

Digital Ocean published a guide on how to mitigate (not fix) the issue: https://www.digitalocean.com/community/tutorials/how-to-protect-your-server-against-the-meltdown-and-spectre-vulnerabilities

You can compare your kernel to their list, uname -r.

14th November, 2017

Zini left a reply on How To Use Database Engine's UUIDs With Eloquent? • 8 months ago

@martinbean Nice, it is more readable than the $cast syntax, but in the case of a database with the ability to generate uuids you shouldn't disable incrementing.

10th November, 2017

Zini left a reply on How To Use Database Engine's UUIDs With Eloquent? • 8 months ago

I think I got what was going wrong:

Eloquent was designed with only incremental ids in mind. To replace a model's incremental id by a uuid, you will find a lot of people on the internet telling you to disable incrementing on your model. This is wrong, uuids are not incremental, but for Eloquent "incremental" doesn't actually mean incremental, it means "being generated by a database trigger" (source) and our uuids should be database-generated.

To use uuids auto-generated on the database level define the model as follows:

class MyModel extends Model
{
    // rename the id column, not mandatory
    protected $primaryKey = 'uuid';

    // tell Eloquent that uuid is a string, not an integer
    protected $casts = [
        'uuid' => 'string'
    ];
}

(in my first tests it seems to work fine)

Zini left a reply on How To Use Database Engine's UUIDs With Eloquent? • 8 months ago

This is the object just after save():

App\User Object ( [primaryKey:protected] => uuid [incrementing] => [casts:protected] => Array ( [uuid] => string )

    [connection:protected] => pgsql
    [table:protected] => 
    [keyType:protected] => int
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 1
    [attributes:protected] => Array
        (
            [name] => Bill
            [updated_at] => 2017-11-10 11:53:39
            [created_at] => 2017-11-10 11:53:39
        )

    [original:protected] => Array
        (
        )

    [changes:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [appends:protected] => Array
        (
        )

    [dispatchesEvents:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [relations:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [timestamps] => 1
    [visible:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [rememberTokenName:protected] => remember_token
)

Zini left a reply on How To Use Database Engine's UUIDs With Eloquent? • 8 months ago

Yes, my model already declares $primaryKey and $incrementing, adding the $casts array doesn't change anything.

The action of creating a new User works, a user is indeed created with its uuid, the problem is that $user->uuidcannot be directly accessed after saving this new user. It can only be accessed after an Eloquent get User::last()->uuid.

9th November, 2017

Zini started a new conversation How To Use Database Engine's UUIDs With Eloquent? • 8 months ago

Hello,

I want to use UUIDs generated by PostgreSQL to avoid relying on a Laravel package for the functionality that is provided directly by the database.

The UUIDs work and can be retrieved afterwards, but Eloquent has a problem doing so directly after saving objects.

The migration defines the PostgreSQL UUID generation:

    Schema::create('users', function (Blueprint $table) {
        $table->uuid('uuid')->primary()->default(DB::raw('uuid_generate_v4()'));
        $table->string('name')->unique();
    });

But when I create a user, I cannot directly access its uuid:

$user = new User;
$user->name = 'test';
$user->save();
$user->uuid     // here it is null, this property doesn't even exist

How can I get my uuid generated by PostgreSQL directly after save()? How does Eloquent proceed in the case of classic ids?

21st March, 2015

Zini left a reply on How To Access Translation Values From Other Locales • 3 years ago

Thanks @usman, it is exactly what I was looking for. You are right @bestmomo, it will be useful the next time.

20th March, 2015

Zini started a new conversation How To Access Translation Values • 3 years ago

Hello, The function trans() allows the access to the values inside the current locale files. I need to do the same, but for any locale. Is this possible with Laravel, or do I have to insert the file manualy Any idea?

17th February, 2015

Zini started a new conversation GrahamCampbell/Laravel-Markdown Render Inline • 3 years ago

I use GrahamCampbell/Laravel-Markdown, a CommonMark wrapper, to render my blocks:

Markdown::convertToHtml($text);

But is there a dedicated method to render an inline block ?

I would like to render Markdown inside blocks where the render should not put block tags, such as <p></p>, around.

Zini left a reply on Laravel 5 Creates A New Session After Each Request • 3 years ago

I installed Laravel again and moved my application inside: now the sessions work how they should. Unfortunately I have no clue where the problem came from.

@blackbird Homestead can be interesting for prototyping, but it is always better to have a local environment as close as possible to the production one.

16th February, 2015

Zini left a reply on Laravel 5 Creates A New Session After Each Request • 3 years ago

@sitesense Yes I have deleted the cookies and even used another browser, but the problem lasts: everytime I reload the auth page, a new session is created.

Zini left a reply on Laravel 5 Creates A New Session After Each Request • 3 years ago

@sitesense You are probably right, also I am moving my app to a new Laravel insallation. I hope it will fix this issue.

In the meantime: this is the configuration of my sessions: 'lifetime' => 120,' expire_on_close' => false,

Zini left a reply on Laravel 5 Creates A New Session After Each Request • 3 years ago

I have the same problem using the last Laravel 5.0: Sessions are created- I can see the files appear in /storage/framework/session - but a new session is created at each request.

With this bug Laravel's sessions are useless, but also all components relying on them, such as the authentication or the flash messages. I did some research, and it seems I am not the only one with this issue.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.