Laravel helpers loaded twice - php

So, I have a Laravel app with a custom helpers. Everything works fine until I run
php artisan route:cache
I got an error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot redeclare getProperty() (previously declared in /Users/xxxx/xxx/xxx/xxx/xxx/a
pp/Helpers/getProperty.php:4)
The helpers are loaded with HelperServiceProvider:
class HelperServiceProvider extends ServiceProvider
{
public function register()
{
require base_path().'/app/Helpers/getProperty.php';
}
}
The HelperServiceProvider is registered in config/app.php providers list:
'providers'=>[
// ...
App\Providers\HelperServiceProvider::class
];
I patched the issue by wrapping my helper with function_exists('getProperty') condition. However it does not seems the right way to do
Any ideas why Laravel is trying to load my helper twice?

Related

Laravel default helper method and facade doesn't work on custom package

I am developing a custom package where I need an access to the config data. I was able to pull data from config through my blade files, however, when I tried to call it from any custom classes I made, it's throwing an error:
Error: Call to undefined function Acme\Package\config()
What's interesting though, is that, when I tried using the facade Illuminate\Support\Facades\Config, it cannot find the class.
Is there any way I could retrieve data from the config (from the package and/or the app)?
<?php
namespace Acme\Package;
class MyClass {
public function test() {
config('app.name');
}
}
UPDATE: It works when running in the browser (package installed in a Laravel project) but fails when running package's test
UPDATE: If this helps, my package can be found here
Call to the config() is from here
And the test case that fails can be found here
<?php
namespace Acme\Package;
class MyClass {
public function test() {
\Illuminate\Support\Facades\Config::get('app.name');
}
}
Try like this, to use the full namespace to the Config Facade. You could also make a use statement under your namespace to inject the facade and then use Config::get('app.name). The reason it is not working is that your package cannot resolve the namespace of that facade as it is outside of the IoC container
You should try this:
<?php
namespace Acme\Package;
use Config;
class MyClass {
public function test() {
Config::get('app.name');
}
}
Updated answer
Please add below line in config/app.php in aliases section
'Config' => Illuminate\Support\Facades\Config::class,
then run below command
php artisan config:cache
php artisan cache:clear

How to create custom controller in Laravel Voyager

I am very new in Voyager.
I have got all the controllers inside TCG\\Voyager\\Http\\Controllers while installing Voyager but didn't find other controllers those I have created using BREAD.
Besides that I want to create custom controller in my Voyager admin panel inside App\\Http\\Controllers\\Voyager . I also followed the steps of Voyager tutorial in Youtube for making custom controller, but couldn't create.
Anybody help please ?
In your config\voyager.php file add your namespace:
'controllers' => [
'namespace' => 'App\Http\Controllers\Back',
],
Then publish voyageres controllers to your namespace
php artisan voyager:controllers
Within that namespace create a new controller derived from VoyagerBreadController
namespace App\Http\Controllers\Back;
use Illuminate\Http\Request;
class SchoolController extends VoyagerBreadController
{
Then you can specify the controller in the bread editor.
NOTE: I did have to refer to mine as Back\SchoolController instead of just SchoolController as I would have expected.
Update:
From version 1.1 now you need to extend VoyagerBaseController instead of VoyagerBreadController.
Add this to your model.
use Illuminate\Database\Eloquent\Builder;
protected static function boot()
{
parent::boot();
static::addGlobalScope('order', function (Builder $builder) {
$builder->orderBy('name', 'asc');
});
}
Try this :
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:cache

Laravel dependency injection does not find the class

I'm using PHPUnit to test my application, in this case I'm testing an API call (I'm doing GET, POST, PUT and DELETE through it). index method responds to GET(/api) route, in this method I have a custom Request:
public function index(\Api\User\Requests\IndexRequest $request)
{
// do some stuff...
}
Api\User\Requests\IndexRequest class looks like this:
class Request extends IndexApiRequest
{
// some methods in here
}
When I execute the test via PHPUnit it prompts:
Class Api\User\Requests\IndexRequest does not exist
Checking the trace route it dies in Illuminate\Routing\RouteDependencyResolverTrait. I couldn't figure out how I can interfere in the execution since it seems to happen between PHPUnit and Laravel.
Does anyone have an idea? I'm using Laravel 5.3, PHPUnit 5.6.5 running on Ubuntu 16.04, PHP 7.0 and nginx.
Thank you!
change class Request extends IndexApiRequest to class IndexRequest extends IndexApiRequest

laravel 5.1 access package config values

I'm starting a package on laravel 5.1. Up till now using laravel 4.2
I've been able to publish the package config file using the following in the boot method of the service provider for my package as described in the documents:
public function boot()
{
$this->publishes([
__DIR__ . '/config/config.php' => config_path('/packages/longestdrive/googlecal/googlecal.php'),
]);
Now I'm trying to access the items in this config using:
config('googlecal.client_id');
This however returns null
If I simply do: config() I get a full array of the config arrays and can see my package config file there.
If I then do: config('longestdrive.googlecal.googlecal.client_id') I can access the variable.
In L4.2 I did not need to add effectively the full path to the variable.
Have I missed something to enable me to simply do: config('googlecal.client_id')
Thanks
You have to merge your config file in ServiceProvider
/**
* Register the service provider.
*/
public function register()
{
$this->app->bind('laravel-db-localization', function ($app) {
return new LaravelDbLocalization();
});
// Load the config file
$this->mergeConfigFrom(__DIR__.'/config/laravel-db-localization.php', 'laravel-db-localization');
}
May be you should run "php artisan config:cache", it works for me:)

PHPUnit 3.7 can't find Controller Classes in Laravel 4.1

Setup 1: Echoing back from the route works in PHPUnit
routes.php
Route::get('signup', function(){
return "Hello World!";
});
/tests/SignupTest.php
class SignupTest extends TestCase {
public function testIndex(){
$this->call('GET', 'signup');
$this->assertResponseOK();
}
}
Running PHPUnit returns the following:
OK (1 test, 1 assertion)
Setup 2: But PHPUnit can't find the Signup Controller when I route to it
routes.php
Route::get('signup', array('uses' => 'Signup#process'));
/app/controllers/Signup.php
class Signup extends BaseController {
public function process(){
echo "Hello World!";
}
}
/tests/SignupTest.php
class SignupTest extends TestCase {
public function testIndex(){
$this->call('GET', 'signup');
$this->assertResponseOK();
}
}
PHPUnit returns the following:
There was 1 error:
1) SignupTest::testIndex
ReflectionException: Class Signup does not exist
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:476
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:416
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:423
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:77
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:50
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:900
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php:118
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:964
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php:934
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:677
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:652
/Applications/MAMP/htdocs/laravel/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:82
/Applications/MAMP/htdocs/laravel/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:319
/Applications/MAMP/htdocs/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:74
/Applications/MAMP/htdocs/laravel/app/tests/SignupTest.php:12
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Setup information:
Laravel 4.1
PHPUnit 3.7.28
Both installed by Composer
MAMP running PHP 5.4.4 on Mac OS X 10.8
I don't think I've changed anything in the core/config files of Laravel or PHPUnit.
Things I've tried:
I found a few answers about this type of issue (both on SO and other sites), where the answers suggested fixing mistakes in names of class/file/method, e.g.:
Laravel Controller not working
I don't think this is an issue here, because I've checked all the names. Also it returns "Hello World!" as expected for both the above routes when tested in a browser, suggesting that the route is working fine in Laravel. Problem seems to be with PHPUnit.
My Question: Why can't PHPUnit find the Signup Class in app/controllers?
I'd assumed that because there was a phpunit.xml file in the basic Laravel install, that PHPUnit would know about it. That assumption turned out to be incorrect.
The Problem
PHPUnit wasn't loading phpunit.xml, which meant the bootstrap file in bootstrap/autoload.php wasn't being loaded, so PHPUnit had no idea where any of the class files were, including Signup.php.
The Solution
I added the file path to phpunit.xml in the --configuration switch when I ran PHPUnit:
--configuration /Applications/MAMP/htdocs/laravel/phpunit.xml
General Advice
If PHPUnit tells you it can't find a class, check that it's loading phpunit.xml and/or your bootstrap file. Make sure your phpunit.xml file points correctly to your bootstrap file and that your bootstrap file is configured correctly to load the required classes.
Also, as pointed out in my question, problems can also be caused by inconsistencies between class/file/method names.

Categories