How do I log properly a Laravel Job? - laravel

Reading the official documentation I understand that it's necessary to use Illuminate\Support\Facades\Log, but the configuration in logging.php is a bit confusing to me. I basically don't understand how the channel drivers work, it seems to me a bit overcomplicated.
Logging commands, however, is pretty easy in my opinion: you just add ->appendOutputTo('command.log') in the schedule method and the job is pretty much done. Is there a similar method to log jobs? Or is the way in the documentation the only one? If so, could anyone be kind enough to simplify it to me?

It is simple to use the Log facade,
Go to loggin.php and add a new channel, here is a signle log file example (without rotation)
'job' => [
'driver' => 'single',
'path' => storage_path('logs/job.log'),
'level' => 'info',
],
Then use it anywhere with
Log::channel('job')->info($content);
//or
Log::channel('job')->error($content);

Related

Laravel Validation 'starts_with' alpha

I am looking to use Laravel validation for user input fields, and all works well aside from one rule I'm looking to enforce: how to ensure a field starts with alpha (where it allows alpha_dash elsewhere). I tried the PHP 'regexp' version [A-Za-z] (shown in code snippet below) but to no avail. I also tried 'starts_with:alpha' also to no avail. I'm hoping to avoid regexp and the like and would rather wait for Laravel solution if there is no simple solution.
Thanks!
'username' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20|unique:users,username',
'firstname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20',
'lastname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:30',
Try this rule instead of alpha_dash & starts_with:
'required|min:2|max:30|regex:/^[A-Z][a-zA-Z0-9_-]+$/'

Skip first index of array validation rule?

Good Evening Devs,
I'm trying to skip the first index of the array while applying validation rule and this is what I tried so far
$validatedData = Validator::make($request->all(),([
'inventories.0' => 'bail',
'inventories' => 'required|array|filled',
'quantities.0' => 'bail',
'quantities.*' => 'required|array|filled',
'required.0' => 'bail',
'required.*' => 'required|array|filled',
]));
But it's not working, any ideas?
I'm trying to add multiple dynamic fields, but want to skip the first index of it.
Please review the picture given below to get the clear picture of the problem.
try this:
$validatedData = Validator::make($request->except(['inventories[0],quantities[0],required[0]']),([
'inventories.*' => 'required|array|filled',
'quantities.*' => 'required|array|filled',
'required.*' => 'required|array|filled',
]));
Bail is not used for skipping an entry. But it may be used for skipping validation logic.
for example,
'phone' => 'bail|numeric|unique:users'
In this case, if somehow the entered phone number is not numeric, it will not check the third validation (i.e. whether the phone number is unique in 'users' table or not).
For your case, you should not use "$request->all()". You should use "request()->except(['inventories[0], quantities[0], required[0]'])" instead
This is perhaps, not the best practice. You're trying to allow the presentation layer to have a direct influence over the data / logic layer of your application. It would probably be better to only send over the data you want to validate rather than sending over everything and they tying to get your validation (and other logic) to ignore the first array element.
Is it an api call or a standard web form you are submitting? If it is an api call, can you not build up your data of only the rows you want to send over, before you make the call?
This will keep your logic layer much cleaner, and allow you to change the ui much easier without affecting the logic, and it being tightly coupled.
Just a suggestion.

zend expressive - middleware checks if there is a next one available?

In my routes.global.php I've this one in routes.
[
'name' => 'test',
'path' => '/test',
'middleware' => [App\Action\Test1Action::class, App\Action\Test2Action::class],
'allowed_methods' => ['GET'],
],
And I've this return $next($request, new JsonResponse($data)); at the end of Test1Action class so it will send the data to the next action.
But is there a way inside Test1Action to check if there's another action after?
Maybe there's another way so I can do the above return if there is one after or return the json response rite away.
return new JsonResponse($data);
That way I can either use Test1Action alone or plug it in before other action.
I tried few options but didn't work. Any help will be great. Thanks.
Maybe you can check $next if it's null or not. But it might always be set, I've never tried it. However in Expressive 2 it's always set and it changes to Delegates. Also the last middleware will always be the NotFoundHandler.
Since you develop your application yourself you know the order of middleware and Actions. I would let Test1Action middleware do it's thing, add the result to the $request as an attribute and let the next middleware figure out if the data was set or not. If the data wasn't set than skip it and execute the next middleware in line. It makes it a lot easier.

Using Route::match() Laravel 5.3

Is it unconventional and therefore ill-advised to use the match() method to determine which controller method to use under 1 uniformed named route? I have this code:
Route::match(['get', 'post'], '/add/lecture/{course}', [
'as' => 'addLecture',
'uses' => Request::isMethod('post') ? 'Main#addLecture':'Main#showAddLecture'
]);
Which works as expected. But I just want to know if this is a feasible solution, or if I should stop being lazy and create two separate routes (I am not using Route::resource() for a particular reason, so please don't advise me to use that for basic CRUD). I don't mean for this question to be subjective, I presume there is an objective reason as to why this isn't employed very often?
It looks like hack. It's not readable and can stop working after random minor Laravel update. In my opinion it's better to create two explicit routes.

Is there a concept of a middleware in CodeIgniter?

I want to do some pre and post processing of requests like handling authentication, loading contextual data, performance timings and things like that. Coming from Django there's a concept of MIDDLEWARE_CLASSES that lets me handle the request in various stages: https://docs.djangoproject.com/en/dev/topics/http/middleware/
Currently it seems like each Controller has to do the same setup and load, in the constructor which isn't ideal because if the constructor fails, the class doesn't get initialized which has subtle but important consequences. I want to move this global handling to a global place.
Any suggestions?
There is not.
This might be helpful.
Codeigniter forum
You need to use hooks for this, Edit your application/config/hooks.php
$hook['post_controller_constructor'][] = array(
'class' => 'Autologin',
'function' => 'cookie_check',
'filename' => 'autologin.php',
'filepath' => 'hooks'
);
I recently stumbled upon this question so even tho it has been 6 years since this question was asked, I am answering in case it helps anybody else like me.
I have found CodeIgniters has "filters" which can be used for that same purpose:
http://codeigniter.com/user_guide/incoming/filters.html?highlight=filters
Actually there is not middleware routing structure in any codeigniter framework until now. But some guys has written a bunch of code to implement that.
I have found it usable for some purposes

Resources