Laravel 4: Autoload Helpers in Modules - laravel-4

I have created multiple modules in the application like:
/app
/modules
events
sections
programs
The problem is i must have to use common helpers like View, Input, Auth etc. in the controller of each module like:
namespace App\Modules\Sections\Controllers;
use View,Input,Auth,Config,Session,Redirect,App,Request;
class SectionsController extends \BaseController{
....
....
}
If i don't add them at the top of the controller then gets below example error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'App\Modules\Sections\Controllers\View' not found
What i need to do so that i don't have to add all those helpers manually in each controller?

The other way to access global namespace is to prefixing the class name with a backward slash:
\Input::all()

Related

How to use this package "https://github.com/kazist/resellerclub-php-sdk" in laravel

While using the above package in laravel I'm getting error as
"Class 'Kazist\ResellerClub\APIs\Controller' not found"
Please suggest me a solution how to call the reseller club api "url" in the controller.
$request = file_get_contents('https://httpapi.com/api/domains/available.json?auth-userid=USER_ID&api-key=API_KEY&domain-name='.$slds.'&tlds='.$tlds.'');
Please help me with a solution how to declare the domain-name and tlds from the above url in laravel.
For package installation:
From terminal go to your project's root directory and run this command:
composer require kazist/resellerclub-php-sdk
And then after successful installation one new folder called kazist will be created inside project's vendor directory.
For using api calls you need to use Guzzle http client https://github.com/guzzle/guzzle or use this link.o
Edit
Yourcontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use
class Yourcontroller extends Controller
{
$resellerClub = \Kazist\ResellerClub\ResellerClub(<userId>, <apiKey>, true); // Last argument is for testmode.
// Get Available TLDs
$resellerClub->domains()->getTLDs();
// Check Domain Availablity
$resellerClub->domains()->available(['google', 'example'], ['com', 'net']); // This will check google.com, google.net, example.com and example.net
}

How to specify a path in routing Laravel?

I have located contoller in directory dashboard, how to specify correct path in routing to this controller?
I have class by path:
Class App\Http\Controllers\Dashboard\PlaceController
But I get error that this class : does not exist
Laravel uses PSR-4 namespaces, so you need to make sure controller is in correct namespace:
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
class PlaceController extends....
If namespace is correct, try to run composer dumpauto command.
Here are a example :
Laravel 5.2 :
create a directory on >> App >> Http >> Controllers >> Dashboard
create a file >>App>>Http>>Controllers>>Dashboard>> PlaceController.php
PlaceController.php
<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
class PlaceController extends Controller
{
// write your functions
}
command line
on command line : composer dump-autoload

Symfony : Attempted to load class

I have install setasign/fpdi on my Symfony, I create a class for generate a PDF :
class MerchPDF extends \FPDI {.... }
and I have this error :
Attempted to load class "FPDI" from the global namespace.
Did you forget a "use" statement?
on local, no problem.
on production, there is this error ... and all files are equals.
Are you an idea ?
Thanks
please have you already try to remove both TCPDF (if it is installed too) and FPDI from the classmap of the composer.json. Then add both: "tecnick.com/tcpdf": "dev-master", "setasign/fpdi": "1.4.2(here the version)", To the "require" part of the composer.json ?
If you require setasign/fpdi as a composer dependency, the base Fpdi class is not available in the global namespace, but in setasign\Fpdi namespace. So your class should start like this:
use setasign\Fpdi\Fpdi;
class MerchPDF extends Fpdi {
...
}
Not sure why it works locally, probably you have installed Fpdi by other means (i.e. not via composer) so it's available as a class in the global namespace.

Laravel 4.2 , Model (eloquent) my own class not found

I have this weird error ... I apparently cannot use any of my model class in my project..
Ad_category model
class Ad_category extends Eloquent {
protected $table = 'ad_category';
protected $fillable = array('*');
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
calling this
$ad_cat=Ad_category::find(1);
error
`Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'Ad_category' not found `
PHP frameworks use a system called "autoloading" to automatically include or require in the correct class definition file when you want to use a class. Autoloading in Laravel 4.2 is in a bit of a transitional spot, which means there's multiple answers to your question.
By default, Laravel 4.2 will look for a class named Ad_category in one of the following four locations.
app/commands/Ad/category.php
app/controllers/Ad/category.php
app/models/Ad/category.php
app/database/seeds/Ad/category.php
That is, Laravel's autoloader will automatically convert Ad_category into the file path Ad/category.php, and then check each configured autoload path for that file. You can configure the base autoloader paths in
#File: app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Laravel 4.2 also uses composer based autoloading. Specifically, is uses a very aggressive form of composer autoloading called classmap autoloading. If you look in your composer.json file, you'll see a section like this
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
When you manually run the command
$ composer dumpautoload
Composer will go through every folder in the above section and look for PHP class files. If it finds one, it adds it to the classmap in vendor/composer/autoload_classes.php. Composer also runs this command automatically during updates.
So, what this means is, if you've defined Ad_category in a different location than Laravel expects to find it, you may be able to get away with running
$ composer dumpautoload
and Laravel will use Composer's autoloader to find your class.

phpunit cannot find Class, PHP Fatal error

I get phpunit and install it as this link using the simplest way for test purposes.
I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin Then, I run phpunit --version, its ok.
PHPUnit 3.7.27 by Sebastian Bergmann
I write a simple test
public function testSomething(){
$this -> assertTrue(true)
}
Then I go into the source file folder, phpunit --colors Test
It works. So, I decide write a complex demo.
My project folder structure is like this.
Project Name
--> app
--> api
--> tests
Now I write a simple class in app/api/FlyApi.php
<?php
class FlyApi {
public function makeFly(){
//do something else
}
}
Then I write another test class for FlyApi.php
<?php
class FlyApiTest extends PHPUnit_Framework_TestCase {
public function testFly(){
//new a FlyApi
$flyApi = new FlyApi();
//do something
}
}
At this line $flyApi = new FlyApi() I got the error.
PHP Fatal error: Class 'FlyApi' not found in /home/kevin/Workspace/fly/app/api/FlyApi.php on line 23
Yes, this line $flyApi = new FlyApi()
You didn't load the definition of your FlyApi class.
This solution is Laravel specific:
You should be extending TestCase rather than PHPUnit_Framework_TestCase.
Try adding your /api/ folder into your ClassLoader at app\start\global.php.
You will find this section:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
...
app_path().'/api/
));
Are you using Laravel's phpunit.xml file? It includes Laravel's (and Composer's) autoload.php file which lets you use all your autoloaded classes within it.
Finally, what's the whole error? It should (hopefully) tell you what class it's trying to load (which will give you clues if the namespace is wrong or something).

Resources