Controller don't work on LARAVEL 5 - php

I started using Laravel today, but I have some problems. Controllers don't run.
This is my controller:
<?php
class HomeController extends Controller {
/*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application welcome screen to the user.
*
* #return Response
*/
public function index()
{
return view('welcome');
}
public function contact(){
return view(pages.contact);
}
?>
and this is my route:
<?php
Route::get('/', function () {
return "hello";
});
Route::get('contact','HomeController#contact');
?>

You need to add the namespace to the beginning of the controller:
<?php
namespace App\Http\Controllers;
You can also run this command when creating a controller
php artisan make:controller HomeController
In, addition as the other answer mentioned, the view name needs to be inside quotes.
Hope this helps.

This should be like this
public function contact(){
return view('pages.contact'); // View name must be inside ' '
}
also you dont need the closing tag for php ?>

Related

Laravel Class 'App\Http\Controllers\Controller' not found

I am new to Laravel and I am trying to fix this error. Controller.php exists in App\Http\Controllers\. I have tried composer dump-autoload and it did not fix it.
I have read that I would need to use artisan to give name to my app. Then it would change namespace from App\ to my app name. Should I do that?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Html\FormBuilder;
use DB;
use App\Http\Controllers\Controller;
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');
}
public function insertform()
{
return view('home');
}
public function insertMeasurement(Request $request) {
$neck = $request->input('neck');
$arm_length = $request->input('arm_length');
$chest = $request->input('chest');
$stomach = $request->input('stomach');
$seat = $request->input('seat');
$shirt_length = $request->input('shirt_length');
$shoulder = $request->input('shoulder');
$arm = $request->input('arm');
$bicep = $request->input('bicep');
$wrist = $request->input('wrist');
$data=array("neck"=>$neck,"arm_length"=>$arm_length,"chest"=>$chest,"stomach"=>$stomach,"seat"=>$seat,
"shirt_length"=>$shirt_length,"shoulder"=>$shoulder,"arm"=>$arm,"bicep"=>$bicep,"wrist"=>$wrist);
DB::table('measurements')->insert($data);
echo "Record inserted successfully.<br/>";
echo 'Click Here to go back.';
}
}
Try composer dump-autoload command once.
Edit : Remove this line class HomeController extends Controller
and replace it with class HomeController extends \App\Http\Controllers\Controller
OR
class HomeController extends App\Http\Controllers\Controller
there is no need for this use App\Http\Controllers\Controller; take it off, your controller should be working fine.
Error can also occur if App/Http/Controllers/ folder does not have Controller.php file.
Make sure file exists.

Auth middleware not redirecting to login page

I have this situation where I have a route /admin which requires Auth middleware to be activated. I have the middleware requirement indicated in the web.php route. Also, I do have the default auth setup of laravel.
The kernel.php does have the middleware indicated too.
But, weirdly enough /admin brings me to a white page with nothing. When logged in, the problem isn't there. It had been working and all of a sudden it was not working anymore.
The auth middleware is as it is:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* #param \Illuminate\Http\Request $request
* #return string
*/
protected function redirectTo($request)
{
return route('login');
}
}
The controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\NewsletterSubscribers;
use App\User;
use File;
class adminController extends Controller
{
//
public function __construct()
{
$this->middleware('auth');
$this->middleware('admin');
}
public function index(){
return view('admin.home');
}
public function changebg(){
return view('admin.changebg');
}
public function changebgimage(Request $request){
$this->validate($request,
[
'image'=>'required|image|mimes:jpg,JPG,jpeg,JPEG|max:4096|dimensions:max_width:1600,max_height:1100',
]
);
$path="images/";
$imagepath="images/bg.jpg";
if( File::exists($imagepath))
{
unlink($imagepath);
}
if ( ! File::exists($path) )
{
File::makeDirectory($path,0777,true);
}
$getimageName = "bg.jpg";
$request->image->move(public_path($path), $getimageName);
return view('admin.home');
}
public function newslettersubscriberslist(){
$newslettersubscribers= NewsletterSubscribers::all();
$count=0;
return view('admin.subscriberslist',compact('newslettersubscribers','count'));
}
public function registerAdmin(){
return view('auth.adminregister');
}
public function viewAdmins(){
$admins= User::select('users.*')->where('role','=','admin')->get();
//print_r($admins);
$count=0;
return view('admin.adminlist',compact('admins','count'));
}
public function viewUsers(){
$users= User::select('users.*')->where('role','=','user')->get();
//print_r($admins);
$count=0;
return view('admin.userlist',compact('users','count'));
}
}
The admin middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class Admin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role == 'admin') {
return $next($request);
}
else {
return redirect('/login');
}
}
}
The the route I'm using:
Route::get('/admin', 'AdminController#index')->name('adminhome')->middleware('auth');
I dont find anything weird here but weirdly enough the problem exists. Can you guys trace somethin unusual here or somewhere it can be??
First of all, make sure you have error reporting turned on. Also take a look at laravel log. Looking at your code the problem might be case of AdminController. In routes you have 'AdminController#index' but the class you showed has name adminController and it should be AdminController. I also don't know what is the name of file but it should be again AdminController.php
You can use the middleware like this
Route::group(['middleware'=>'auth'], function()
{
Route::get('/admin', 'AdminController#index')->name('adminhome');
}

Laravel - FatalThrowableError Class 'App\Http\Controllers\Input' not found

I'm new to Laravel so I'm not familiar with errors in the framework .I'm trying to get the user to make a post but I'm getting the above error .Could you please tell where I'm going wrong ?Thank you
This is my HomeController class:
<?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\Http\Response
*/
public function index(Request $r)
{
if(Input::has('status-text'))
{
$text = e(Input::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
And this is my web.php class :
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses' =>'HomeController#index']);
Don't use Input::get(), use $r->get() as you're injecting the request as a dependency to the index method already, and Input:: is merely a alias to access the underlaying Request.

NotFoundHttpException in RouteCollection.php (line 179)

The problem that I'm having in my web app is that when a user types up a status and presses "add status" instead of it saving the status I get the following error : NotFoundHttpException in RouteCollection.php (line 179).
I have done some research online and looked at similar problems and tried changing it around but I still got the same problem back .
This is my Controller Class
<?php
namespace App\Http\Controllers;
use Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $r)
{
if(Request::has('status-text'))
{
$text = e(Request::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->user_id = Auth::user()->id;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
This is my web.php class
<?php
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses'=>'HomeController#index']);
This is an image of the error that I'm getting back :
First thing is you can just have return view() in the routes/web.php file. All you should need to do is make your web.php like this:
Auth::routes();
Route::get('/home','HomeController#index');
that should fix your problem.

Class App\Http\Controllers\Requests\ArticleRequest does not exist Laravel5.3

Actually I'm very new of the php and Laravel5.3,and I have this problem when i want to set request of the form(Doing a simple blog page)
ReflectionException in Route.php line 339: Class App\Http\Controllers\Requests\ArticleRequest does not exist
And this is my controller code(Filename:ArticlesControllers.php):
<?php
namespace App\Http\Controllers;
//namespace App\Http\Controllers;
use App\Http\Requests\ArticleRequest;
use App\Article;
use Carbon\Carbon;
use Illuminate\Http\Request;
class ArticlesControllers extends Controller
{
//
public function index(){
$articles = Article::latest()->get();
//return 'articles';
return view('articles.index')->with('articles',$articles);
}
public function show($id){
$article = Article::find($id);
// if(is_null($article)){
// abort(404);
// }
//dd($artilce);
return view('articles.show',compact('article'));
}
public function create(){
return view('articles.create');
}
public function store(Requests\ArticleRequest $request){
//dd($request->all());
//接受post过来的数据
//存入数据库
//重定向
$input=$request->all();
//$input['published_at']=Carbon::now();
Article::create($input);
return redirect('/articles');
}
}
And the request File code:(Filename:ArticleRequest.php in the path Requests)
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ArticleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title'=>'required|min:3',
'content'=>'required',
'published'=>'require'
];
}
}
My Route/Web.php is:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Route::get('/','SiteController#index');
Route::get('/', function () {
return view('welcome');
});
Route::get('/articles','ArticlesControllers#index');
Route::get('/articles/create','ArticlesControllers#create');
Route::get('/articles/{id}','ArticlesControllers#show');
Route::post('/articles/store','ArticlesControllers#store');
How can i get rid of this nasty problem,I've been searching on StackOverFlow but nearly all the answers may not solve it....
Change it to this to make it work:
public function store(ArticleRequest $request)
you already imported the class at the top
use App\Http\Requests\ArticleRequest
no need to use a full class name as an argument
just use
public function store(ArticleRequest $request)
or
public function store(\App\Http\Requests\ArticleRequest $request)

Categories