I have two controller file homecontroller and backendcontroller. What is the best way to create global function and access it from both files?
I found here Arian Acosta's answer helpful but I wonder if there is an easiest way. I would appreciate any suggestions.
Solution
One way to do this is to create a class and use its instance, this way you can not only access the object of the class within a controller, blade, or any other class as well.
AppHelper file
In you app folder create a folder named Helpers and within it create a file name AppHelper or any of your choice
<?php
namespace App\Helpers;
class AppHelper
{
public function bladeHelper($someValue)
{
return "increment $someValue";
}
public function startQueryLog()
{
\DB::enableQueryLog();
}
public function showQueries()
{
dd(\DB::getQueryLog());
}
public static function instance()
{
return new AppHelper();
}
}
Usage
In a controller
When in a controller you can call the various functions
public function index()
{
//some code
//need to debug query
\App\Helpers\AppHelper::instance()->startQueryLog();
//some code that executes queries
\App\Helpers\AppHelper::instance()->showQueries();
}
In a blade file
Say you were in a blade file, here is how you can call the app blade helper function
some html code
{{ \App\Helpers\AppHelper::instance()->bladeHelper($value) }}
and then some html code
Reduce the overhead of namespace (Optional)
You can also reduce the overhead of call the complete function namespace \App\Helpers by creating alias for the AppHelper class in config\app.php
'aliases' => [
....
'AppHelper' => App\Helpers\AppHelper::class
]
and in your controller or your blade file, you can directly call
\AppHelper::instance()->functioName();
Easy Solution:
Create a new Helpers folder in your app directory.
Create a php file named your_helper_function.php in that Helpers directory.
Add your function(s) inside your_helper_function.php
function your_function($parameters){
//function logic
}
function your_another_function($parameters){
//function logic
}
Add this file to the Files key of your composer.json like
"autoload": {
...
"files": [
"app/Helpers/your_helper_function.php"
]
...
}
Finally, regenerate composer autoload files. (Run this in your project directory)
composer dump-autoload
That's it! and now you can access your_function() or your_another_function() in any part of your Laravel project.
If you still have any confusion, check my blog post on how to do this:
How to Add a Global Function in Laravel Using Composer?
Updated:
Step 1
Add folder inside app folder
app->Helper
Step 2
add php Class inside Helper folder
Eg. Helper.php
Add namespace and class to the Helper.php
namespace App\Helper;
class Helper
{
}
Register this Helper.php into config/app.php file
'aliases' => [
....
'Helper' => App\Helper\Helper::class
]
Now, write all the functions inside Helper.php and it will be accessible everywhere.
How to access from Controller?
Step 1 - Add a namespace at top of the controller.
use App\Helper\Helper;
Step 2 - Call function - Assume there a getInformation() inside the Helper Class.
$information = Helper::getInformation()
In your Controller.php which extends BaseController, you can create a function like;
public function data($arr = false)
{
$data['foo'] = 'bar';
return array_merge($data,$arr);
}
And from any controller when you send a data to a view;
public function example()
{
$data['smthg'] = 'smthgelse';
return view('myView',$this->data($data));
}
The data in the the main controller can be accessed from all controllers and blades.
The Laravel Service Provider way
I've been using global function within Laravel for a while and I want to share how I do it. It's kind of a mix between 2 answers in this post : https://stackoverflow.com/a/44021966/5543999 and https://stackoverflow.com/a/44024328/5543999
This way will load a file within a ServiceProvider and register it within your Laravel app.
Where is the difference, the scope, it's always about the scope.
Composer //Autload whitin composer.json method
|
|--->Laravel App //My method
|
|--->Controller //Trait method
|--->Blade //Trait method
|--->Listener //Trait method
|--->...
This is a really simplist way to explain my point, all three methods will achieve the purpose of the "Global function". The Traits method will need you to declare use App\Helpers\Trait; or App\Helpers\Trait::function().
The composer and service provider are almost about the same. For me, they answer better to the question of what is a global function, because they don't require to declare them on each place you want to use them. You just use them function(). The main difference is how you prefer things.
How to
Create the functions file : App\Functions\GlobalFunctions.php
//App\Functions\GlobalFunctions.php
<?php
function first_function()
{
//function logic
}
function second_function()
{
//function logic
}
Create a ServiceProvider:
//Into the console
php artisan make:provider GlobalFunctionsServiceProvider
Open the new file App\Providers\GlobalFunctionsServiceProvider.php and edit the register method
//App\Providers\GlobalFunctionsServiceProvider.php
public function register()
{
require_once base_path().'/app/Functions/GlobalFunctions.php';
}
Register your provider into App\Config\App.php wihtin the providers
//App\Config\App.php
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
...
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
App\Providers\GlobalFunctionsServiceProvider::class, //Add your service provider
Run some artisan's commands
//Into the console
php artisan clear-compiled
php artisan config:cache
Use your new global functions
//Use your function anywhere within your Laravel app
first_function();
second_function();
Laravel uses namespaces by default. So you need to follow the method described in that answer to setup a helper file.
Though in your case you want to access a method in different controllers. For this there's a simpler way. Add a method to you base controller app/Http/Controllers/Controller.php and you can access them in every other controller since they extend it.
// in app/Http/Controllers/Controller.php
protected function dummy()
{
return 'dummy';
}
// in homecontroller
$this->dummy();
There are a few ways, depending on the exact functionality you're trying to add.
1) Create a function inside Controller.php, and make all other controller extend that controller. You could somewhat compair this to the master.blade.php
2) Create a trait, a trait can do a lot for you, and keeping ur controllers clean. I personally love to use traits as it will look clean, keep my Controller.php from being a mess with tons of different lines of code.
Creating a global function
create a Helpers.php file under a folder, let's name it 'core'.
core
|
-- Helpers.php
namespace Helpers; // define Helper scope
if(!function_exists('html')) {
function html($string) {
// run some code
return $str;
}
}
In your composer.json
"autoload": {
"psr-4": {
},
"files": [
"core/Helpers.php"
]
}
in the file that you want to use it
// the " use " statement is not needed, core/Helpers is loaded on every page
if(condition_is_true) {
echo Helpers\html($string);die();
}
Remove the namespace in Helpers.php if you want to call your function without the need to prefix namespace. However I advise to leave it there.
Credit: https://dev.to/kingsconsult/how-to-create-laravel-8-helpers-function-global-function-d8n
By using composer.json and put the function containing file(globalhelper.php) to the autoload > files section, then run
composer dump-autoload
You can access the function inside the file(globalhelper.php) without having to calling the class name, just like using default php function.
I had anonymous component resources\views\components\homepage\feedback.blade.php to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
class Feedback extends Component
{
public $feedbacks;
public function __construct()
{
$this->feedbacks = Feedback::wherePublished(true)->take(5);
}
public function render()
{
return view('components.homepage.feedback');
}
}
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:\laragon\www\lara7\resources\views\components\homepage\feedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
I found problem.
I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\views\components\homepage\feedback.blade.php and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.
So laravel tried to render resources\views\components\homepage\feedback.blade.php with $feedback variable inside and it cannot find where $feedback is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/> to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working.
But it doesn't say that inside components subfolder automatic discovery is not working.
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue
I am working on a Yii1 old website. which is linked with some external PHP controllers. These external controllers provide some common functions that are used between 2 different applications. I have a function in Yii model that I want to use in one of the external PHP controller is there a way to do this? Currently, this is done by rewriting MySQL query in the PHP external controller but I don't want to follow this lame practice.
I found this link and I am able to access Yii externally but it's still not very helpful. Using Yii in 3rd-Party Systems
Here's a sample of my code:
namespace main\Helpers;
require_once('path/to/yii.php');
Class HelperClass {
public static function yiisupport($id){
// I am able to access Yii variables using
\Yii::app()->name
// But how to access the yii model or controller functions? I need something like the follwoing
$model = \Yii::app()->YiiModel::model()->findByPK($id);
}
}
Can anyone help?
You need to create Yii application first (using config file path) to access its models and controllers as it is mentioned in the documentation. Then you can access any model class in your external application just like you would access it in your Yii application, and you can use controller actions as below;
$controller = new \YOURController('ACTION_NAME');
$controller->ACTION_NAME();
If you have already imported models/controllers in your config file then you will not need to import any class but if you have not then you can import specific model/controller like below;
\Yii::import('application.models.MODEL_NAME');
\Yii::import('application.controllers.CONTROLLER_NAME');
Check the examples below;
namespace main\Helpers;
require_once('path/to/yii.php');
\Yii::createWebApplication('path/to/config.php');
Class HelperClass {
public static function yiisupport($id){
// Access Yii variables
\Yii::app()->name;
// Access yii model
$model = \YiiModel::model()->findByPK($id);
// Access yii controller and its actions
$controller = new \YiiController('actionCreate');
$controller->actionCreate();
}
}
Update:
As mentioned by #rob006 in the comment below that calling yii controller action outside Yii application is a bad idea, however if you still want to do that, there is a safer way which follows the Yii application lifecycle and this way access filters and beforeAction() will be triggered. So you can call controller action in a safer way as below;
\Yii::app()->runController('route/to/action');
Hi i'm newbie in Laravel just practicing with some tutorial videos and i'm wondering if it is possible to configure the brackets position when i use the artisan commands.
For example when i create new controller using artisan make:controller it makes
class Controller extends Controller
{
//
}
What i want is the brackets places right next to the class declaration not in the new line like above. To be more specific
Class Controller extends Controller{
//
}
This is what i want. Is it possible to configure the brackets position?
I've googled a lot and read documentation but couldn't find any of information about it.
--Edited--
Thanks to you guys i know it's not Laravel problem and it can be configured in code editor.
I'm using vs code and have installed several php formatter but it seems all of them follow the PSR-2 styles.
I found JS configuration for its brackets position but couldn't find for PHP
i know it's not even problem i just don't like this format
As mentioned in the comments, the stubs that Laravel generates follow PSR-2 so making this change would break that.
To override the controller code that Laravel generates with make:controller you're going to have to override the ControllerMakeCommand and copy the default stubs you want to edit.
The stubs for this command have changed quite a bit with the different versions of Laravel so it will require a bit of copy and paste to ensure it works correctly.
Extend the ControllerMakeCommand
Inside your app/Console/Commands directory create ControllerMakeCommand.php and add the following:
<?php
namespace App\Console\Commands;
use Illuminate\Routing\Console\ControllerMakeCommand as BaseCommand;
class ControllerMakeCommand extends BaseCommand
{
//
}
Add the getStub method
Find the getStub() method in the Illuminate\Routing\Console\ControllerMakeCommand and copy that into your newly created ControllerMakeCommand.
Copy the default stubs
Copy
vendor/laravel/framework/src/Illuminate/Routing/Console/stubs
to
App/Console/Commands/stubs
(you don't have to copy all of the files inside it, just the controller.* that you want to override).
Change the formatting of the files as you see fit.
Laravel <=5.4
If you're using Laravel <=5.4 commands are not automatically loaded for you so you're going to have to tell Laravel that you want to use this command.
In your App\Console\Kernel.php add your new class to the $commands array e.g.:
protected $commands = [
\App\Console\Commands\ControllerMakeCommand::class
];
NB Obviously, feel free to store the stubs wherever you want just make sure you update the paths in your new ControllerMakeCommand.
I have a laravel install, setup as a new CMS.
The frontend of the site is a pretty complex old website. The new CMS is going to take over a portion of it, but migrating everything to Laravel isn't an option.
I understand that I can use https://packagist.org/packages/illuminate/database to enable Eloquent and make database queries. However, what this doesn't include is the models I've already created as part of the CMS.
So on the same server I have:
/var/www/laravel-cms/ <- Laravel based CMS
/var/www/website.com/ <- Custom PHP Project
How can I use my models created in laravel-cms on website.com?
Is there anything stopping you from copying ( or perhaps simlinking) your legacy models into the /var/www/laravel-cms/app directory and then using namespaces to call those models from the the controller?
namespace App\Http\Controllers;
...
use App\Legacy-Models\Model1 as Example;
class ExampleController extends Controller {
public function index() {
$ex = new Example();
...
You can create some method in your Laravel controller using the Laravel model. then call the function from website.com using Curl.