BadMethodCallException Method App\Http\Controllers\SiteController::index#index does not exist - php

I can not find the error:
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller{
public function index(){
return 'Home page do site';
}
}
My Route:

You have not defined the route. we define route "web.php" of our laravel file.
Route::get('/index', 'SiteController#index')->name('home');
this migght work for you.

Related

Laravel function does not exists

I'm having some problems with my Laravel APP.
I'm using laravel 8 and every time when I try to visit home page it gives me like:
Function () does not exist
This is how my routes looks like:
Route::get('/{any}', [VueController::class])->where('any', '.*');
And here how VueController looks like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function __invoke()
{
return view('application');
}
}
VueController::class second parameter is missing it should be
Route::get('/{any}', [VueController::class,'index'])->where('any', '.*');`
then in controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class VueController extends Controller
{
public function index()
{
return view('application');
}
}
Route::get() get req second array must have function name like [VueController::class,'index']
if you don't want to mention index then in resouncse it is possible but it generate some url with some function it will not work in __invoke()
ref link https://laravel.com/docs/8.x/routing

Laravel Lumen Call to undefined function App\Http\Controllers\responce()

When I try to call API then I got this error.
Here is My Controller
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class VideoController extends Controller
{
public function index(){
//return 'index function of video controller';
$Video = DB::table('videos')->get();
return responce()->json($Video);
}
}
?>
here is my web.php
$app->get('/', 'VideoController#index');
You should return $Video or any other variable or array, not a function response()
EDIT
try to just return json_encode($Video);

Call the UI function in controller class in laravel

UI Code: in resources\views\DistributorRegistration.php
<?php
class DistributorRegitrationForm
{
public function distributorRegitrationFormHtml(){
return '<h1>Hello</h1>';
}
}
?>
In Controler Class.....
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use resources\views\DistributorRegistration;
class DistributorRegistration extends Controller
{
function VestigePOS_GRNHandler(Request $request){
$id = $request->input('id');
return view(DistributorRegitrationForm::distributorRegitrationFormHtml()) ;
}
}
When I call this controller in routes
Fatal error: Class 'App\Http\Controllers\DistributorRegitrationForm' not found
Which file contains the class DistributorRegitrationForm? I'm missing an use App\...\DistributorRegitrationForm; in the controller, if it's not in the same namespace.
Calling DistributorRegitrationForm::distributorRegitrationFormHtml() won't work, unless the method becomes a static method (public static function).
You have some typos in there ;-)

Laravel: Returning a view from a controller

I'm trying to learn how to use Laravel 5, but I've bumped into a problem. I've created the following code so far:
under app/HTTP/routes.php:
<?php
Route::get('/', 'MyController#home');
Created my own MyController.php file under app\Http\Controllers and added the following code to the controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class MyController extends BaseController
{
public function home()
{
$name = "John Doe";
return View::make("index")->with("name", $name);
}
}
When I run the app I get the error:
FatalErrorException in MyController.php line 12:
Class 'App\Http\Controllers\View' not found
What am I doing wrong?
Change
return View::make("index")->with("name", $name);
To
return \View::make("index")->with("name", $name);
or Even better
return view("index",compact('name'));
UPDATE
View is a Facade, a wrapper class, and view() is a helper function that retrives a view instance.

Laravel 5 Model namespace class not found

I am trying to store my models in a custom namespace and directory structure as shown here:
I have:
namespace Modules\Core;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
But I am getting:
FatalErrorException in TestController.php line 8:
Class 'Modules\Core\User' not found
Which is wrong anyway, so I thought it must be 'Modules\Core\Models\User'. I tried this and I still got the same error (just with a different class name).
My model:
namespace Modules\Core;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User Extends Eloquent {
protected $table = 'users';
}
How can I get access to this model in the TestController?
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => 'TestController#index']);
});
If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\Core\Controllers;
And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\Core\Models;
Then, in the controller, import it before using it:
<?php namespace Modules\Core\Controllers;
use Modules\Core\Models\User;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
I had the same problem as above. In my case I had the following namespace:
namespace Modules\XMLApi;
I got the same error as above. When I changed the namespace to the following:
namespace Modules\XmlApi;
Then run the following command:
composer dump-autoload
Then it worked.
You should edit your routes.php file:
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => '\Modules\Core\TestController#index']);
});
to use full together with namespace

Categories