I´m using Laravel 5.2, and pass to my controller method the $request:
namespace App\Http\Controllers;
use Input, Session, Exception, Request;
class WebController extends Controller {
public function myfunction(Request $request) {
$request->fullUrl();
}
}
But Laravel return me this error:
Call to undefined method Illuminate\Support\Facades\Request::fullUrl()
In the docs for the 5.2 version all is right:
https://laravel.com/docs/5.2/requests
This method fullUrl exits in the Request.php file ...
What is the problem?
You are importing the Request Facade, which is resolving to a Request facade instance when you type hint in your controller's method.
Instead, import the actual Request object:
use Illuminate\Http\Request;
If you must use the facade as well, you can do something like:
use Illuminate\Http\Request;
use Request as RequestFacade;
Just remove Request from use Input, Session, Exception, Request;
and add new line:
use Illuminate\Http\Request;
So final code look like:
namespace App\Http\Controllers;
use Input, Session, Exception;
use Illuminate\Http\Request;
class WebController extends Controller {
public function myfunction(Request $request) {
$request->fullUrl();
}
}
Hope this help you well!
Related
Am new in consuming api. am trying to fecth data from https://jsonplaceholder.typicode.com/posts in Laravel but am getting error related to class not imported even though i have imported the Http Class used in the Laravel Docs.
this is my controller ApiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class ApiController extends Controller
{
public function fetch()
{
$response = Http::get('https://jsonplaceholder.typicode.com/posts');
dd($response);
}
}
this is the error am receiving
"Class 'Illuminate\Support\Facades\Http' not found"
I have already installed the installed the Guzzle package. What am i doing wrong please.
You seem to be using the Laravel 7 way of using Guzzle. Change your Controller like so to make it work in Laravel 5.8
<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
class ApiController extends Controller
{
public function fetch()
{
$client = new Client;
$request = $client->get('https://jsonplaceholder.typicode.com/posts');
$response = $request->getBody();
dd($response);
}
}
Edit: To get the contents of the request use dd($response->getContents()); instead of dd($response);
I am trying to extend the package controller in my base laravel controller. Tried importing class using below code which shows error as class not found.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ProductController as ControllersProductController;
use App\Imports\ProductsImport;
use AvoRed\Framework\AvoRedProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Avored\Framework\Catalog\Controllers\ProductController;
class ProductControllers extends Controller
{
private $avored_product;
public function __construct(ProductController $p) {
$this->avored_product = $p;
}
public function index() {
echo $this->avored_product;
}
Tried multiple options by researching on it couldn't find the same. Request all to please guide me with same.
Could you share with us the exact error message? From your code snippet I can't see which class couldn't be found.
And which Avored package are you referring to? I guess avored-laravel-ecommerce?
If you want to extend the ProductController-from the package, you have properly extend from that controller.
<?php
namespace App\Http\Controllers\Admin;
use Avored\Framework\Catalog\Controllers\ProductController as AvoredProductController;
class ProductControllers extends AvoredProductController
{
public function index() {
// Do your thing in here
}
}
You can now override the controller methods to your liking.
Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.
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
I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}