How to shorthand imports of Models in PHP Lumen - php

I tried short handling my models in my controller since they are in the same folder. So Instead of doing these:
namespace App\Http\Controllers\Api;
use App\User;
use App\Pictures;
use App\Info;
I tried these:
namespace App\Http\Controllers\Api;
use App\{
User, Pictures, Info
};
Unfortunately it's not working. It shows that the controller doesn't read it when I short handed it. Any suggestions or reasons why this doesn't work?

In PHP V7.0, You can refer this method http://php.net/manual/en/language.namespaces.importing.php
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};

Related

Laravel api route class to found error

I'm currently working on api jwt based authentication off a boiler plate on SitePoint. So far I've gotten everything working but I'm stuck on this point.
My Controller looks like this:
namespace App\Api\V1\Controllers;
use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;
use Symfony\Component\HttpKernel\Exception\HttpException;
use JWTAuth;
use App\Http\Controllers\Controller;
use App\Order;
// use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
in the body I have this function:
public function checkThis()
{
$currentUser = JWTAuth::parseToken()->authenticate();
$orders = App\Order::first();
echo $orders;
function() {
echo "stll here";
};
}
Under my api route I have this in a middleware:
$api->get('orderlist', 'App\\Api\\V1\\Controllers\\OrderController#checkThis');
When I run this in postman I get the following error: "message": "Class 'App\Api\V1\Controllers\App\Order' not found",
I've tried everything I can think of and it still keeps happening. When I take it out of the controller and run it directly in the routes it works. I'm a newbie to Laravel and PHP so I'm kinda of stuck.
All thing below assusing that you want to invoke the App\Order::first();
In function checkThis , you can replace App\Order::first() by
Order::first() //aliasing version
or replace App\Order::first() by
\App\Order::first(); //fully qualified Name version
By php manual
Example #1 importing/aliasing with the use operator
<?php
namespace foo;
use My\Full\Classname as Another;
// this is the same as use My\Full\NSname as NSname <-- very important
use My\Full\NSname;
Notice that
// this is the same as use My\Full\NSname as NSname <-- very important
use My\Full\NSname;
another php manual
and Inside a namespace, when PHP encounters an unqualified Name in a
class name, function or constant context, it resolves these with
different priorities. Class names always resolve to the current
namespace name. Thus to access internal or non-namespaced user
classes, one must refer to them with their fully qualified Name
php manual : fully qualified Name
Fully qualified name
This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar. The namespace \Foo is also a
fully qualified name.
So if you want to invoke the function App\Order::first ,just Order::first,for the reason that
use App\Order;
equal
use App\Order as Order ;
the aliasing is Order instead of App\Order . And the Fully qualified name is \App\Order instead of App\Order.
On the other hand , when you invoke App\Order::first();
it means that you are invoking
App\Api\V1\Controllers\App\Order::first();
So it can't not find the class;
Here is my demo
file1.php
<?php
namespace App;
class Order{
public static function first(){
echo "i am first";
}
}
file2.php
<?php
namespace App\Api\V1\Controllers;
include 'file1.php';
use App\Order ;
\App\Order::first();
// and you can do it by
// Order::first(); they are equal in this place
when i run command php file2.php
it echo i am first

How to use methods from one controller in another

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work
LanguageRepository::findByLocale(1);
This is what I am using in beginning of my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
use Waavi\Translation\Repositories\TranslationRepository;
use Illuminate\Foundation\Application;
If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
class DefaultController extends Controller
{
private $language_repository;
function __construct(LanguageRepository $language_repository)
{
$this->language_repository = $language_repository;
}
public function index()
{
dd($this->language_repository->findByLocale("en"));
}
}
Note: you need pass language string instead of id to findByLocale method. see line 97

How to use Laravels spatie/laravel analytics package

I just downloaded this package for Laravel.
spatie/laravel-analytics
Its a Google Anayltics package, and I followed all the steps for setting up an account. What I'm having trouble is calling the methods. For example when it says:
Here is an example to retrieve visitors and pageview data for the current day and the last seven days.
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
I tried doing this in my function like this:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Analytics\Analytics;
use Illuminate\Support\Collection;
class DashboardController extends Controller {
public function index() {
$analytics = Analytics::fetchVisitorsAndPageViews(Period::days(7));
dd($analytics);
return view('admin.dashboard-v2');
}
}
Its giving me errors like:
Non-static method Spatie\Analytics\Analytics::fetchVisitorsAndPageViews() should not be called statically
Am I missing something here? I couldn't find any specific documentation online except for the Github ReadMe file
If you want to use the facade to access the class, you'll need to change use Spatie\Analytics\Analytics; to use Analytics;. That should take care of that error.
If you are going to use Period::days(7) then you will need to add use Spatie\Analytics\Period; because that's an actual static method, not a facade.

Importing class in laravel 5.2

use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
do i need to write the above import class every time when i create a new controller?In laravel 4.2 it does automatically
is any other way to do this?
Yes, you do. Your app is under the namespace App;, so if you want to use the Input class, but you don't say use Illuminate\Support\Facades\Input, PHP will try looking for it under your namespace App; and an exception will be thrown since the class you're looking for probably won't be there.
Those are not a mandatory class.
When ever you are using those facedes you have to add
There is one way i know.
class something like CustomController
Add the all the common classed in that controller.
Now for every controller you can extend the new CustomController

performance and the use statment PHP, Laravel 4.x

Are there performance considerations when importing classes with the use statement in PHP and more specifically laravel 4.x ?
for example often times i have many use statements in my controllers as such:
use OrganisationController;
use Input;
use Redirect;
use Validator;
use View;
use Organisation;
use Sentry;
use User;
use Str;
use Lang;
use Application;
use Job;
use Upload;
class ApplicationController extends OrganisationController {
...
In my opinion, it should not effect on performance because, when you use a class that is not in the same namespace you use it with a preceding backslash like
\View::make(...);
This way you just telling that, the View class is within the global namespace, to make the code clean you can use use statement top of your class like
use \View, \Redirect, \Validator; // more
class WhatEver extends BaseController {
function index()
{
//...
return View::make(...);
}
}
So, in either way, php is loading (include/require) the class whenever it founds any use of the class but when it's not in the scope of current namespace you must provide the namespace and it could be directly from the code like
\View::make(...);
Or maybe using a use statement to keep your code clean but in both ways, php is loading the class during run time (if not already loaded) but you have to provide the right location (with namespace).

Categories