I checked controller.php in Laravel 5 and it only has a base controller. In Laravel 4 you also have a home controller. Is the home controller removed in Laravel 5?
L5 comes with no HomeController like previous versions. But, you can create a new controller using the command
php artisan make:controller HomeController
or you can manually create one. But make sure you are extending the Controller.php class.
eg:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
Related
I was trying to register user using the Laravel Auth. First I got error with App\User not found then I fix it with App\Models\User it works. I don't really know what with Laravel 8 becuase I never had a problem with user registration with previous version. Then I got this problem
BadMethodCallException
Method App\Http\Controllers\HomeController::home does not exist.
I don't really know which code to provide since I don't even touch the default code of the Authentication/HomeController.
But I did changed the namespace in RouteProvider
protected $namespace = 'App\Http\Controllers';
web.php
Route::get('/home', [HomeController::class,'home']);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
in your web.php top you need to import
use App\Http\Controllers\HomeController;
then u can use
Route::get('/home', [HomeController::class,'index']);
Or else
Route::get('/home', 'HomeController#index')->name('home');
use this here no need to import
Note - your error is showing u don't have home method in your controller so create home method or change the correct method by default it is index
Try Route::get('/home', [HomeController::class,'index']);
I've this this model City.php in app\Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
//
}
and this CityController.php
<?php
namespace App\Http\Controllers;
use App\Models\City;
use Illuminate\Http\Request;
class CityController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$cities = City::OrderBy('city_code')->get();
return view('testcities',compact('cities'));
}
but i get back this error in laravel:
Error Class 'App\Models\City' not found
You should rename cities.php to Cities.php. But based on Laravel best practices Model names MUST be in singular form with its first letter in uppercase. So it's better to create model name with City rather than Cities.
Please make sure your class is named Cities.php. You can also try to run composer dump-autoload to tell Composer to read all your classes again.
I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');
I am working in laravel 5.5. I have created a controller folder with name Abc and inside it created a controller file with name abc.php, inside this file I have written code:
namespace App\Http\Controllers\Abc;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\DB;
class abcController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function NewPromotion()
{
return view('new-promotion');
}
}
And in Route folder under web.php file I am calling its view file, like:-
Route::get('/new-promotion','Abc\abcController#NewPromotion');
In spite of defining in namespace I am still getting error i.e.
"ReflectionException (-1)
Class App\Http\Controllers\Abc\abcController does not exist".
What could be the possible issue?
You should make sure your file is located in app/Http/Controllers/Abc directory and has name abcController.php (case sensitive).
Also by convention class names start with big letter, so you should rather name your controller AbcController and have this file with name AbcController.php (and also update your route file to use valid case of this controller name).
Good way to create controller is use cmd (you have to be in project directory)
php artisan make:controller "Abc\abcController"
I am developing a simple web application with Laravel 5.1 and my development environment is Homestead.
I have a view composer to pass Auth::user() data to admin panel related views automatically. Most general admin panel pages (Dashboard, Settings etc.) uses AdminController, and it extends Laravel's Controller. Specific admin panel pages (Users, Orders etc.) has their own controllers (Admin\UsersController, Admin\OrdersController respectively), which are extends AdminController.
No any middleware registered in routes for admin panel related routes, instead AdminController loads the auth middleware (which checks if registered user tries to load the page). And no other controller that extends AdminController overrides the constructor.
My problem is that if user is not logged in and tries to load an admin panel page (doesn't matter which one; Dashboard, Settings, Users, Orders - because the view composer called for every single one to pass Auth::user() data) there is no warning says "You are not authorized." or no redirection to login page, just an exception thrown which says Auth::user() is null.
Doesn't the auth middleware called first? If not what should I do to prevent the exception to be thrown (returning from view composer is not an elegant solution for my point of view by the way)?
Thanks in advance.
Addendum
AdminController
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function __construct()
{
// After middlewares
$this->middleware("auth");
$this->middleware("admin");
// Before middlewares
$this->middleware("no-cache");
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view("admin.index");
}
}
Admin\OrdersController
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
class OrdersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$orders = Order::with("address")->get();
return view("admin.orders.index")->with("orders", $orders);
}
}
ComposerServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Auth;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
view()->composer("layouts.admin.default", function ($view) {
$admin = Auth::user();
$view->with([
"admin" => $admin,
"picture" => $admin->pictures[0]
]);
});
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Note: ComposerServiceProvider is registered in config/app.php.
Your Admin\OrdersController extends App\Http\Controllers\Controller, when it should extend App\Http\Controllers\Admin\AdminController. That's why you are not getting a redirection.