Where to write common function or logic in laravel 4.2 - laravel-4.2

I want to use a common function or logic in all controllers on laravel4.2, what would be best way to do this? Does Anyone have any idea?

You can use Helper for that.
Create helpers.php in app folder.
You can create normal function there which you can use anywhere in laravel.
Example syntax for function :
function YOUR_FUNCTON_NAME(param1,param2) {
#YOUR_CODE
}
After creating helpers.php file , add it in app/start/global.php file.
like below :
require app_path().'/helpers.php';
And you can use anywhere in your controller like this YOUR_FUNCTON_NAME(param1,param2)
Hope this help.

Related

How do I call a FrontController from within a module

I implemented a Prestashop module for Payment
beside of my main Module I generated a ModuleFrontController , which work fine and I have access on it by using URL like mydomain.com/module/controllers/mymethod
The problem is I would like to execute this method from my main module class / a simple php file
But I can not find a solution for that yet.
Note :
I Can not use CURL / open_socket
I can not call this URL from an Ajax
Simply I want to make an instance of my FrontController and call a method
Any Idea?
Thx
Include that controller file where you want to use and create instance of class and call desired method on that
FrontController define like this :
class ModulenameControllernameModuleFrontController extends ModuleFrontController
{
...
}
and make didn't accept like normal instance to call $obj = new Classname()
,... or when some how make an instance , wasn't work well. was missing some parameters in $this->context->XXX
Any way my problem solve some have different without need to make an instance .
But I think should be another way like how Cron execute
Ex. for Cron can send parameter like :
$_GET['fc'] = 'module';
$_GET['module'] = 'modulename';
$_GET['controller'] = 'controllername';
,....
Some how to call FrontController , independently

Laravel scope not responding

I am using laravel for my backend api.
My question is about an scopefilter, the problem is that it is not responding when I call to it.
I have a lot of examples for using scopefilters.
So I looked at each of them to see if I did something wrong.
But I can't seem to find the problem.
When I call to this model in laravel, I use a parameter to define too the scopefilter to use a specific function.
The point only is that it never gets to this function, I don't get a response when I have put a log in this function.
I assume it is a syntax problem but maybe someone else can find the problem for this.
public static $scopeFilters = [
"supplierArticleClientId" => "bySupplierArticleClientId"
];
public function scopeBySupplierArticleClientId($query, $clientId) {
\Log::info([$clientId]);
}
In this case I expect that I see an clientId in my log.
You have to create a Custom validation function Implementing Rule Class
please go through this link for reference

Need explanation on Maatwebsite Excel "From View" code

While researching on how I can import table listing data using collection + HTML view to an excel file, I came across maatwebsite/excel package. I managed to import the excel by following the documentation. However, I am not familiar with the code syntax used by the author. On this link, the author defines a function view() as seen in the code below:
public function view(): View
{
return view('exports.invoices', [
'invoices' => Invoice::all()
]);
}
I am not familiar with view(): View syntax. Can someone direct me to a proper documentation for this? And tell me how I can resolve this in Netbeans (See screenshot below)?
Screenshot of Netbeans error
Please keep in mind, the code itself is working correctly and has no issues. I am able to download the excel file as per my requirements.
Using: Laravel 5.6, PHP 7.1.17, NetBeans 8.0.2, maatwebsite/excel 3.0
Any info on this is appreciated. Thanks in advance.
In PHP 7.0 and greater, we can define the return type. For example.
function sum(): int {
// function body.
}
And same goes for:
public function view(): View
This line says that the function view() only returns the View class object.

Yii global variables and setting issue

I am developing a social networking website using Yii. While frequently using the following things I am having great data manageability issue.
- User ID
- Current user ID (the user which profile is the owner viewing)
- Is owner???
where can I define these things.
I would something like
if(Yii::app()->owner==ME){
//do something
}
// and similarly
if($this->isMyFreind(<Current user ID>){
}
// $this(CanIView()){
}
I want these functions to be public for any page? But how?
In other words
Where can I put my library which contains my own favorite functions like text shortening, image cropping, date time format etc etc??
In Yii, you can do achieve this by making a class (under protected/compoents) which inherits
CApplicationComponent
class. And then you call any property of this class globally as a component.
class GlobalDef extends CApplicationComponent {
public $aglobalvar;
}
Define this class in main config under components as:
'globaldef' => array('class' => 'application.components.GlobalDef '),
And you can call like this:
echo Yii::app()->globaldef->aglobalvar;
Hope that can help.
According to the MVC model, things like image cropping or date time formats would go in models. You would simply create models for that.
I usually use a globals.php file with all my common functions
In the index.php (yip the one in the root):
$globals='protected/globals.php';
require_once($globals);
I can then call my global functions anywhere. For example, I shorten certain Yii functions like:
function bu($url=null){
static $baseUrl;
if ($baseUrl===null)
$baseUrl=Yii::app()->request->baseUrl;
return $url===null ? $baseUrl : $baseUrl.'/'.ltrim($url,'/');
}
So I can then call the Yii::app()->request->baseUrl by simply calling bu()

Creating filters for auto_html

I want to create a custom filter for auto_html. Where do I put the filter so I can use it?
The documentation doesn't touch on any of that. Thanks!
The approach I took was to add the code to an initializer file such as:
/path/to/your/application/config/initializers/auto_html.rb
Then you can just write something like:
AutoHtml.add_filter(:change_colours).with({}) do |text, options|
text.gsub("#FF0000", "#00FF00")
end
And call auto_html(input) { change_colours } in your model. The empty hash will take any options you care to pass to the filter.