Is there a concept of a middleware in CodeIgniter? - 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

Related

How do I log properly a Laravel Job?

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);

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.

How to do PUT and DELETE with Zend_Http_Client

The Zend_Http_Client docs are confusing and hard to understand. Currently I am using the following code to GET information from the Challonge API:
$client = new Zend_Http_Client("https://api.challonge.com/v1/tournaments/$bracket.json");
$client->setParameterGet(array(
'api_key' => $apikey,
'include_participants' => 1,
));
$feed = $client->request()->getBody();
Very simple, three lines. Now this is a GET. How would I do the same exact thing as a PUT? Passing parameters and everything. What about a DELETE?
Sorry, I know this is not directly related to the question Json Axelrod asked, but I had a similar problem and could not find the solution anywhere online.
I was trying to do a PUT / DELETE request with Magentos Varien_Http_Client
class Varien_Http_Client extends Zend_Http_Client
So I thought the same would apply that was written in this topic and here. However no matter what I tried I could not get PUT nor DELETE requests to work.
Really simple solution in that case: Use Zend_Http_Client instead of Varien_Http_Client.
It seems that Magentos Http Client class is adding some extra "convenient" methods for preparing the body that won't allow PUT nor DELETE requests.
You would do
$client->request('POST')
or
$client->request('DELETE')

Basset 4 (pre-beta)

I'm using Basset 4 to manage assets.
In the config file i'm declaring a collection 'admin'
return array(
'collections' => array(
'admin' => function($collection)
{
$collection->directory('assets/js', function($collection)
{
$collection->add('vendor/jquery-1.9.1.min.js');
});
},
),
...
)
later in a view, I would like to add an extra file in admin collection.
I've try the following code, but it doesn't work:
Basset::collection('admin', function($collection)
{
$collection->add('function.js');
});
Is there a way to add file into a collection from a view or from a controller?
Thank you
Basset isn't really designed to work like that. You should be defining all your assets within that initial call, even though the ability to add assets throughout the execution of an application is possible, it's not recommended.
When building a collection assets that are added for a particular route won't be available to the builder since Artisan doesn't fire any routes, etc.
Adjusting a collection in numerous places can often lead to confusion further down the line.
I know this isn't ideal as you're probably looking at implementing page specific JavaScript, correct? I've thought about it but can't really think of a clean solution (suggestions?), although I've heard of people assigning a unique ID to the body or perhaps some classes that their JavaScript can then attach themselves to.
It's not brilliant but that's the best I can give you at the moment.

How to set up FubuMVC validation

I'm trying to learn FubuMVC and have gotten stuck on validating my input models.
What I want to accomplish is post-validate-redirect. That is, to redirect to same view and show the errors if the model is invalid. I'm using attributes on my models.
Also, how would I specify my own error messages, i.e localization?
I'm using the latest packages of Fubu from nuget.
My registry looks like this:
IncludeDiagnostics(true);
Applies.ToThisAssembly();
Actions.IncludeClassesSuffixedWithController();
Routes
.HomeIs<HomeController>(x => x.Index())
.IgnoreControllerNamesEntirely()
.IgnoreMethodsNamed("Index")
.IgnoreMethodsNamed("Query")
.IgnoreMethodsNamed("Command")
.IgnoreNamespaceText("Features")
.IgnoreMethodSuffix("Html")
.RootAtAssemblyNamespace()
.ConstrainToHttpMethod(x => x.Method.Name.EndsWith("Command"), "POST")
.ConstrainToHttpMethod(x => x.Method.Name.EndsWith("Query"), "GET");
this.UseSpark();
this.Validation();
HtmlConvention<SampleHtmlConventions>();
Views.TryToAttachWithDefaultConventions();
The FubuMVC.Validation package is really just an example of how to use FubuValidation as we haven't built it out for all of the edge cases. Having said that, let me explain a little bit about how it works so we can see if you can use it, or if you should just handroll your own validation behavior.
The ValidationBehavior uses the IValidationFailureHandler interface to "handle" validation failures. The Notification object built up from FubuValidation is shoved into the IFubuRequest when the behavior fires, and then the handler is called.
The ValidationFailureHandler class is wired up by default for all
validation failures. This delegates to the IValidationFailurePolicy to
determine the strategy to use for a given model type (see my post on
policies for an explanation of how this works).
The Validation extension method has an overload which gives a micro-
dsl for configuring these policies:
this.Validation(x => {
x.Failures....
});
From here you can 1) apply custom policies via the ApplyPolicy method
or 2) use the predicate based configuration approach via the IfModel methods.
If you go the predicate route (e.g., x.Failures.IfModelIs()), you can tell FubuMVC.Validation to use FubuContinuations to redirect or transfer to another behavior
chain. Rex recently posted about FubuContinuations if you're looking for some guidance in this area (http://rexflex.net/2011/07/fubumvc-fubucontinuation/).
Hope this helps and feel free to ask away if I didn't explain anything enough,
Josh

Resources