Where to put custom global functions in CakePHP? - php

I want to add implementation of array_column() for CakePHP app that is currently running on PHP 5.3 (array_column() has been introduced in PHP 5.5). What would be the best method of doing it without touching core files?
Please note that I don't want to use helper, I really want to define global function.

If you don't want to write a helper there are a few places where you can put global functions. If you're going to use the functions only in your controllers the best place is the AppController. In a similar way you could use the AppModel class for model functions and for the views you could put them within a layout.
But if you want to use the functions across the entire application, I would recommend putting them inside the app/Config/bootstrap.php file.

Related

Laravel - Use function in blade

I'm beginning to build an app using Laravel, and one of the challenges I'm now facing is that throughout my blade views, i need to show a bunch of different bootstrap modals. Because of this, i believe it would be a better approach to create some sort of helper method that takes a few parameters to define how the modal should look. But im currently not sure what is best practice of doing this. Am i even allowed to use such a helper method in a view?
How would you do this?
You can still process any data using the PHP tags (<?php echo 'hi'; ?>), or even using the complete namespace of the file and then call the desired function, like {{ \Carbon\Carbon::today() }}
You can create a helper file and then define methods which can be used globally, including the views. But in your case the ideal scenario would be to use a partial and then to include the partial with different parameters to change the properties. This would work exactly like a method, but cleaner and more aligned with views. This should help you, the sub-views are called partials.
https://laravel.com/docs/5.4/blade#including-sub-views

Laravel: Will Helper functions work for it?

I am using Multiple code blocks of Mail:: and Gate::define() in my code. I rather want to make it as functions and call them. What approach would be useful? Should I create helper classes?
If You have some logic and you want to use this logic from many places, you must to encapsulate it, an in your situation it's not bad solution to create helper function or maybe helper class.
If You don't know how - here is very clear example to do it :
Creating a Helpers File

CodeIgniter: where should I put custom utils?

I'm using CodeIgniter and I have a few utils that I want each controller (that is, each controller file) to have access to.
The question is: where to put these?
I thought of helpers, but the CI documentation talks only about extending existing helpers, not making your own. I'm sure that doesn't mean I can't make my own helpers, but it does mean I don't know how they should be built (procedural? Methods of the global CI instance? etc)
I also considered hooks, but this is a poor fit I think as I'm not extending core functionality.
Or is there some other way I'm missing?
It's been a while since I've done this but I believe I used two approaches.
Creating a new, custom helper that goes into /application/helpers, following steps noted from this answer: CodeIgniter: Create new helper?
Creating a new library class into /application/libraries which I also activate in the autoload configuration found in /applications/config/autoload.php. This way it's always available to my controllers when I need it. CI has good documentation on this one (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html).
I did it simply by adding a file to the application/helpers folder (maybe I created that folder - I can't remember) and then loading them in the usual way.

Where to put big, rarely used functions in codeigniter

I have a several functions which are quite large, and are only used in one controller function each, and I'm wondering where to put these? They are not displaying any views, but instead crunching some numbers.
If I'm not wrong, there are 4 possible places where i could put these function: in my controller, in a helper, in a library or in a model. But none of these seem appropriate, since I don't want the code to be loaded every time a user uses the controller, and model should be used to do database stuff, and helpers and libraries should contain code that can be used over and over again.
If it is business logic, the best place to put it is in the controller as a private method, then you can call that method from within the controller.
Just as a note, helpers aren't always loaded unless you autoload them or load them in the constructor of your controller. So, as an alternative, you can make these methods of a helper then just load the helper in the controller action you wish to use them. That way they only get loaded when you need them.
CodeIgniter comes with helpers that you probably might not use (doesn't load unless you specify it in the application/config/config.php file) and I don't think its a problem having functions that you only use once stored there (application/helpers ). For example I might use a random password generator once only, but its still there and won't be loaded unless I call it.
$this->load->helper('my_string_generators');

proper place of Configuration::load() in CakePHP

I wish to load some global defaults in my CakePHP application. E.g. app/config/defaults.php
$config['site']['name']='FooBar';
$config['secuity']['somelimit']=-1;
I'm going to use (Configuration::read()) these values across various classes not just in a single controller.
Where is the proper place to call Configuration::load('defaults')? In the router? In the subclass of App?
To clarify my objective: I would like Configuration::load('defaults') for loading global defaults. But I would not like to do this in every Router, Helper etc. Is there a global hook/constructor for CakePHP applications that is executed before anything else?
I put that call into app/config/config.php

Categories