Http\Request and Facades\Request conflict - laravel

How to use Http\Request and Facades\Request together same time.
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Request;
In my custom login, I have used Laravel-Throttle not to attempt login too many times. The problem is when not using Http\Request error shows
FatalThrowableError Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request
and if using Facades\Request, following error occurs
ErrorException Non-static method Illuminate\Http\Request::instance() should not be called statically"
Here is my code block,
I am using laravel 5.4

You can alias one of conflicting namespace and use it
use Illuminate\Http\Request as HttpRequet;
use Illuminate\Support\Facades\Request;
Then when you need to use Request within the Illuminate\Http namespace you can simply use HttpRequest
Or if you can inline the namespace

Call the class directly to avoid namespace issues: \Illuminate\Http\Request::instance();

Related

Basic OOPS concept needs clarification

I am new to OOPS and MVC hence, have confusion about certain concepts which I would like to clarify. As mentioned below in the code, I think, namespace App\Http\Controllers\Admin; that is mentioned corresponds to the directory structure and thus means the class AdminController is contained in Admin folder as is pointed there. But then we have the use keyword inorder to use the following namespace. Now the question is why do we use use App\Http\Controllers\Controller;, particular line. What purpose does it serve?
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
namespace App\Http\Controllers\Admin; as you stated is used to let laravel know where to access your AdminController from.
And your AdminController which extends the base Controller uses the default classes of base Controller so we need to use the use specification. use App\Http\Controllers\Controller; is used to let declare that you are going to use the base Controller classes in your AdminController.
For more info read the official laravel docs.
use usually allows a developer to shorten the namespace.
It copies another class from same or different namespace so you can use that class in your code with its class name. You need to write the full namespace if you will use another class without use.
This might help you understand it better. https://daylerees.com/php-namespaces-explained/
namespaces basically group your functions, classes and constants under a particular 'name', which we call a namespace.
Now use keyword allows a developer to shorten the namespace.use is useful when we are going to call the same function again and again in the same code file or call different functions, constants or classes under a particular namespace.

Class 'App\Http\Controllers\PHPExcel_Style_Protection' not found on laravel

In laravel I want protect cell, but a get error Class 'App\Http\Controllers\PHPExcel_Style_Protection' not found, I use MaatWebsite class. this my code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
class ExportExcellDaftarhadirController extends Controller
{
Excel::create('exsport', function($excel) {
$excel->sheet('Daftar', function($sheet){
$sheet->getProtection()->setPassword('password');
$sheet->getProtection()->setSheet(true);
$sheet->getStyle('A12:B20')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
$sheet->loadView('export_excell.daftarhadirujian')
});
})->export('xls');
}
Try using \PHPExcel_Style_Protection::PROTECTION_UNPROTECTED instead of PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
You need to use the properly-namespaced path to PHPExcel_Style_Protection. I don't know the path as I'm unfamiliar with the Maatwebsite library but, as PHP is currently warning you, PHPExcel_Style_Protection isn't found within the App\Http\Controllers namespace.
If you're unfamiliar with namespaces, php's namespace documentation should help clarify their purpose and usage.

Using phpseclib in model laravel 5.0

I'm trying to include Crypt/RSA.php in my model, but this always appear "AppClass 'App\Crypt_RSA' not found". Any idea? Thanks in advance
That error appears because you're trying to instantiate a the Crypt_RSA class in the App namespace where it doesn't exist, because the Crypt_RSA class is defined in the global namespace. So you can do one of two things:
1. Write a use statement at the top of your file:
use Crypt_RSA;
2. Prepend a backslash to the class name when using it:
new \Crypt_RSA();
You can read more on how namespaces work in the PHP Namespaces Documentation.

Class App/Http/Controllers/View Not Found error

I am new to laravel 5 and currently stumped by this error:
FatalErrorException in TicketController.php line 18: Class 'App\Http\Controllers\View' not found
Weird thing is the view does in fact exist, i checked to see if the route was indeed routing to the right controller and it was, the error pops up when i try to do this:
return View::make('tickets.bus.index');
It's either i am making some mistake somewhere or if the implementation is different from laravel 4
The problem is not the actual view but the class View. You see when you just reference a class like View::make('tickets.bus.index') PHP searches for the class in your current namespace.
In this case that's App\Http\Controllers. However the View class obviously doesn't exists in your namespace for controllers but rather in the Laravel framework namespace. It has also an alias that's in the global namespace.
You can either reference the alias in the root namespace by prepending a backslash:
return \View::make('tickets.bus.index');
Or add an import statement at the top:
use View;
In Laravel 5.1 the correct use code would be:
use Illuminate\Support\Facades\View;
There exists a helper-function, view(), which is in the global namespace, and may be used to simplify the syntax:
return view('tickets.bus.index');
With this method, it is unnecessary to include use View; or include the root namespace, e.g., \View.
The concepts that lukasgeiter explained are essential to understanding Laravel, even if you elect to use the helper-function.
For me it was namespace problem. I used php artisan to create controller but it seems like php artisan used different namespace (may be I have to change something in composer.json to fix it but I am totally new in laravel)
Whoops, looks like something went wrong.
FatalErrorException in PagesController.php line 11:
Class 'App\Http\Controllers\Controller' not found
Good that I am using phpStorm which automatically inserted proper namespace
make sure you check out namespace properly. This is how I had controller created with php artisan
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller; //php artisan inserted.
class PagesController extends Controller
{
public function index(){
return view('index');
}
public function about(){
return view('pages.about');
}
}
and this is how phpstorm inserted after I manually wrote extends controller
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Routing\Controller; //I manually wrote extends Controller and selected this namespace
class PagesController extends Controller
{
public function index(){
...
There exists a helper-function, view(), which is in the global namespace, and may be used to simplify the syntax:
return view('tickets.bus.index');
With this method, it is unnecessary to include use View; or include the root namespace, e.g., \View.
The concepts that lukasgeiter explained are essential to understanding Laravel, even if you elect to use the helper-function.

Namespacing and autoloading in Laravel

This might be a simple question but I'm wondering how do I autoload useful classes without declaring use statements on every single file.
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
public function index()
{
Input::get('query');
}
}
If I remove the use Illuminate\Support\Facades\Input; line I will get a class not found error because I'm using the Input class.
Is there a way to autoload useful classes like Input, Response, View like in Laravel 4. What's the point of the Aliases in app.php?
You can import input class using both:
use Illuminate\Support\Facades\Input;
or
use Input;
then you can use Input::get('query'); code. That's how PHP namespaces work - you can also look at How to use objects from other namespaces and how to import namespaces in PHP for more details about it.
If you don't use use statement for importing class, you can use \Input::get('query'); or \Illuminate\Support\Facades\Input::get('query');.
Aliases allow you not to use fully qualified classes for example \Illuminate\Support\Facades\Input but shorter form \Input. That's why I showed above 2 versions - the shorter one uses aliases and the longer uses full class path. The same mechanism is both in Laravel 4 and Laravel 5 I believe.
The problem is not really in Laravel, but in PHP. When you namespace a class, it assumes that everything inside that class will be in the same namespace, so you have to tell it that, for a particular class, you need it to use a different namespace.
You can use them by referring to the root namespace, like this:
class HomeController extends Controller
{
public function index()
{
\Input::get('query');
}
}

Resources