How to "import" namespaces in Laravel? - laravel-4

I've built an API in Laravel, versioned as follows:
/controllers/api/v1/CommentController.php
/controllers/api/v2/CommentController.php
In my routes I call the correct controllers like so:
Route::resource('notification', 'api\v2\CommentController');
This works, but since I'm using namespaces in my controllers, I have to use the \ approach in order to find classes like \Response in the root namespace?
namespace api\v1;
class NotificationController extends \BaseController {
public function index()()
{
return \Response::json({});
}
}
Is there a way to avoid that backslash notation and using the right namespace?
I tried using use \App; but without result

U need to add "use Response;" to your code.
<?php namespace api\v1;
use Response;
class CommentController extends \BaseController {
public function index()
{
return Response::json('hello');
}
}

Related

Laravel Livewire error when I add a constructor to call a service class

I've got a piece of code I want to reuse. I've read this Laravel cleaner code article and this other Laravel Services Pattern article, where I have realized I can reuse code in several places of the application by using services classes.
In this case, I created a new MyService class, inside a new folder app/Services/MyService.
namespace App\Services;
class MyService
{
public function reuse_code($param){
return void;
}
}
The problem comes when I want to call the class through the constructor inside a Livewire class component, as follows:
<?php
namespace App\Http\Livewire;
use App\Services\MyService;
use Livewire\Component;
use Livewire\WithPagination;
class LivewireTable extends Component
{
use WithPagination;
private $myClassService;
public function __construct(MyService $myService)
{
$this->myClassService = $myService;
}
public function render()
{
$foo = $this->myClassService->reuse_code($param);
return view('my.view',compact('foo'));
}
}
The error displayed is the following:
Argument 1 passed to App\Http\Livewire\LivewireTable::__construct()
must be an instance of App\Services\MyService, string given
(However, If I use a trait, there are no problems. But I am afraid then my traits collide as previous experiences)
How do I fix it? What am I missing?
Livewire's boot method Runs on every request, immediately after the component is instantiated, but before any other lifecycle methods are called
Here's the solution worked for me.
Solved
Just like #IGP said, reading in the livewire docs it says:
In Livewire components, you use mount() instead of a class constructor
__construct() like you may be used to.
So, my working code is as follows:
<?php
namespace App\Http\Livewire;
use App\Services\MyService;
use Livewire\Component;
use Livewire\WithPagination;
class LivewireTable extends Component
{
use WithPagination;
private $myClassService;
public function mount(MyService $myService)
{
$this->myClassService = $myService;
}
public function render()
{
$foo = $this->myClassService->reuse_code($param);
return view('my.view',compact('foo'));
}
}

How to use namespaces in case of inheritance?

I am new to OOPS thus want to clarify things. I have this code below which works perfectly fine.
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Now I don't intend to use use keyword as it is going to be used once also wanted to experiment and thus, I used the code below.
namespace App\Http\Controllers\Admin;
class AdminController extends App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Now the above mentioned code without use keyword throws a fatal error exception. Why does that happen? In theory,I think I am doing exactly what is supposed to be done then why the exception?
You will have to import it from global namespace , below code will work fine
namespace App\Http\Controllers\Admin;
class AdminController extends \App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Use keyword import class from global namespace, but
class AdminController extends App\Http\Controllers\Controller
it will import parent class relative you current namespace (namespace App\Http\Controllers\Admin) , so translated path will be: App\Http\Controllers\Admin\App\Http\Controllers\Controller which is invalid.

Dependency resolving with App::make not working when bind is done by a provider in Laravel

I'm using Laravel 5.2. I tried to resolve a dependency in Laravel out of the IOCContainer as follows.(with App::make method)
App/FooController.php:-
<?php
namespace App\Http\Controllers;
use App\Bind\FooInterface;
use Illuminate\Support\Facades\App;
class FooController extends Controller
{
public function outOfContainer(){
dd(App::make('\App\bind\FooInterface')); // Focus: program dies here!!
}
}
Bindings for the FooInterface done in the AppServiceProvider as follows
App/Providers/AppServiceProvider.php:-
<?php
namespace App\Providers;
use App\Bind\Foo;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('\App\Bind\FooInterface', function() {
return new Foo();
});
}
}
Structure of the Foo class as follows.
App/Bind/Foo.php:-
<?php
namespace App\Bind;
class Foo implements FooInterface {
}
Structure of the FooInterface interface as follows:-
<?php
namespace App\Bind;
interface FooInterface {
}
Then I created a route as follows.
Route::get('/outofcontainer', 'FooController#outOfContainer');
But when I navigate to this route it throws an exception with the error text:
BindingResolutionException in Container.php line 748:
Target [App\bind\FooInterface] is not instantiable.
What is going wrong with this?
How to use App:make() with the AppServiceProvider?
In your service provider, you're binding the string '\App\Bind\FooInterface'. In your controller, you're attempting to make the string '\App\bind\FooInterface'. These strings are not the same, as they have differing cases (Bind vs bind). Since the strings are not the same, Laravel cannot find the binding in the container.
Correct the casing in your make statement and it should work:
dd(App::make('\App\Bind\FooInterface'));

Repository pattern Laravel 5

I am starting to learn about Laravel 5 repository pattern, and I can't get anything right! I probably used wrong namespace or 'use'; This is the error i am getting: Class App\Http\Controllers\ServiceController does not exist
This is my Controller:
namespace App\Http\Controllers;
namespace Repo;
use App\Http\Controllers\Controller;
use Repo\ServiceRepository;
class ServiceController extends Controller {
public function __construct(ServiceRepository $service){
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Here's the repository:
class ServiceRepository {
public function getAllServices(){
return \App\Service::all();
}
}
And here's my route:
Route::get('/services', 'ServiceController#showServices');
I've tried to change some namespaces but i end up with more errors :/
You have to read a bit about namespaces in PHP.
If your ServiceController is located in app/Http/Controllers directory, then the code has to be like follows:
<?php
namespace App\Http\Controllers;
use Repo\ServiceRepository;
class ServiceController extends Controller
{
public function __construct(ServiceRepository $service)
{
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Note that I removed namespace Repo and the first use statement.
A class has to have only a single namespace, so I removed the second Repo namespace.
Namespaces are really simple. In most cases namespace of a class just reflects a path to that class. In your case, it's App\Http\Controllers because your controller is probably located in app/Http/Controllers directory.
I've removed use App\Http\Controllers\Controller because your ServiceController and App\Http\Controllers\Controller are in the same directory, so they are in the same namespace. When two classes are in the same namespace, a full path with a namespace is not necessary for them to be able to access each other.
Hope that this helps.

how to access to laravel global Classes in packages

I am developing a Laravel package . In the packeges I need to File::delete for delete files, but The following error message is displayed :
Class 'Chee\Image\File' not found
Can you help me?
You have to declare it in the top of your class:
namespace Chee\Image;
use File;
class Whatever()
{
}
Instead of using the File Facade, you can also get it directly from the Laravel IoC container:
$app = app();
$app['files']->delete($path)
In a service provider, you can inject it as a dependency your package class:
class Provider extends ServiceProvider {
public function register()
{
$this->app['myclass'] = $this->app->share(function($app)
{
return new MyClass($app['files']);
});
}
}
And receive it in your class:
class MyClass {
private $fileSystem;
public function __construcy($fileSystem)
{
$this->fileSystem = $fileSystem;
}
public function doWhatever($file)
{
$this->fileSystem->delete($file)
}
}
You should be able to just use:
\File
Namespaces are relative to the namespace you declare for the class you are writing. Adding a "\" in front of a call to a class is saying that we want to look for this class in the root namespace which is just "\". The Laravel File class can be accessed this way because it is an alias that has an declared in the root namespace.
Assuming you have file something like that:
<?php
namespace Chee\Image;
class YourClass
{
public function method() {
File::delete('path');
}
}
you should add use directive:
<?php
namespace Chee\Image;
use Illuminate\Support\Facades\File;
class YourClass
{
public function method() {
File::delete('path');
}
}
Otherwise if you don't use PHP is looking for File class in your current namespace so it is looking for Chee\Image\File. You could look at How to use objects from other namespaces and how to import namespaces in PHP if you want

Resources