I'm learning Laravel 5.4 and I can't get around this. I've added two routes in my view blade like this
Write post
Then in my route web.php file I have
Route::resource('/backend/blog', 'Backend\BlogController');
In HomeController#index where I loading index page which has the button above, like this
<?php
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
class BlogController extends BackendController
{
protected $limit = 5;
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::with('category', 'author')->latest()->paginate($this->limit);
$postCount = Post::count();
return view("backend.blog.index", compact('posts', 'postCount'));
}
...
}
HomeController in Backend dir holds
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends BackendController
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('backend.home');
}
}
backend.home has the a href above which generates the error.. Why this happen?
Full error message
ErrorException in UrlGenerator.php line 304:
Route [backend.blog.create] not defined. (View: /var/www/blog/resources/views/backend/home.blade.php)
Route::resource('/backend/blog', 'Backend\BlogController', [
'names' => [
'create' => 'backend.blog.create'
]
]);
route() helper make named route
routing
Route::POST("/backend/blog/create", "Backend\BlogController#create")->name("backend.blog.create");
in view
{{ route('backend.blog.create') }}
Related
I am working with laravel RestfulApi project. I am facing an unexpected trouble. When I try to send an API request to api.php route, it goes to the web.php route. But if I don't use validation in my controller file, the code runs well. I only get the above problem when using validation. Below is my code.
Api.php Routes
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/showingData','UserContactController#show');
Route::post('/storingData','UserContactController#store');
Web.php Routes
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
UserContactController.php
<?php
namespace App\Http\Controllers;
use App\UserContact;
use Illuminate\Http\Request;
class UserContactController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'name'=>'required|max:5'
]);
$userContact=new UserContact();
$userContact->name=$request->input('name');
$userContact->email=$request->input('email');
$userContact->description=$request->input('description');
$userContact->visibility=$request->input('visibility');
$userContact->created_by=$request->input('created_by');
$userContact->save();
return response()->json([
"message"=>"student record created"
],201);
}
}
UserContac.php Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserContact extends Model
{
//
}
----------
My Postman url
http://amaderproject.test/api/storingData
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.
I created middleware: php artisan make:middleware CheckUserStatus
In this middleware I have:
namespace App\Http\Middleware;
use Closure;
class CheckUserStatus
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if(Auth()->check() AND Auth()->user()->status === 0) { // user is logged in but it is blocked
auth()->logout();
return redirect('/');
}
return $next($request);
}
}
Then, one of my controller I have:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckUserStatus;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware('CheckUserStatus');
}
}
This gives ReflectionException - Class CheckUserStatus does not exist
What I'm doing wrong ?
You need to register your middleware if you want to reference it by a string key. Check out the docs here.
Alternatively, you could use the fully qualified class name: try CheckUserStatus::class instead of 'CheckUserStatus'.
You need to use the fully qualified class name:
Either:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware('\App\Http\Middleware\CheckUserStatus');
}
}
or
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Middleware\CheckUserStatus;
class productsController extends Controller
{
public function __construct () {
$this->middleware('auth');
$this->middleware(CheckUserStatus::class); //No quotes
}
}
You need to add your middleware in kernel.php
protected $routeMiddleware = [
'your_desire_name'=>\App\Http\Middleware\CheckUserStatus::class,
];
I am trying to print the current route in my controller
namespace findetrip\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($page = 'home')
{
echo $route = Route::current();
return view('admin.'.$page,['pageName'=>$page]);
}
}
But I got the following error:
Class 'findetrip\Http\Controllers\Route' not found
I found many questions similar to this issue, but didn't get a proper solution.
To use Route::current(), you have to use Route like:
use Illuminate\Routing\Route;
Note:
Look at your app.php, you should have this on 'aliases' array:
'Route' => "Illuminate\Support\Facades\Route",
use Illuminate\Routing\Controller;
Use these code in the below of your controller and try it.
To use Route::current(), you have to include Route class in your controller:
use Route;
I need to get the id and Name about Costumers to DropDown in Laravel5 , in Laravel4 I use the following code.
public function create()
{
$data = ['groups' => Cliente::lists('name', 'id')];
return view('projects.create')->with('data',$data);
}
After , In the View I show with the following
{{ Form::select('group', $groups) }}
But in the controller Client I have this
<?php namespace App\Http\Controllers;
use App\Project;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class ClienteController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$projects = Cliente::all();
return $projects;
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
}
When I execute the view create , I can see the following error
Class 'App\Http\Controllers\Cliente' not found
Finally I use this but I need to show the name and id about client in the view not only the id, any solutions for this ?
public function create()
{
$data = ['Selecciona el ID del ususario' => \DB::table('clientes')->lists('id','id')];
return view('projects.create')->with('groups',$data);
Then , the view.
{!!Form::select('user_id', $groups) !!}