Laravel Route [blood-camp] not defined exception - php

I'm getting this error even though the route is defined
Route [blood-camp] not defined. (View: C:\wamp64\www\blood-donation\resources\views\layouts\app.blade.php)
web.php file
`Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home.root');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/user', [App\Http\Controllers\UserController::class, 'index'])->name('user');
Route::resource('blood-camp', BloodCampController::class);
Route::resource('donor', DonorController::class);
Route::resource('camp-schedule', CampScheduleController::class);`
app.blade.php
<li class="nav-item dropdown">
<a class="nav-link" href="{{ route('blood-camp') }}" role="button" >
{{ __('Camps') }}
</a>
</li>
controller class
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BloodCampController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('blood_camp.index', ['camps' => DB::table('blood_camps')->orderBy('name', 'ASC')->paginate(10)]);
}
//all the other resources are there...
}
route:list
There is no duplicate route
I have tried all the suggested ways including changing path names, cache clearing, private window, restarting server, but I'm still getting this error and no way to go further developments.
It first occurred when I adding new route called camp, I changed the name to blood-camp but no luck, now it throw exceptions for the other routes as well. Can someone please explain me what I'm doing wrong?

As the result of your route:list shows, you dont have a route with the alias blood-camp.
What you have as alias is blood-camp.index, blood-camp.store....
To generate the link of the ressources listing use:
By path/url
{{ url('blood-camp') }}
Or by alias
{{ route('blood-camp.index') }}

Related

Laravel 5.6 Route Group

My controllers which are HomeController and BlogController in Admin folder. My views like:
/admin
index.blade.php
/blog
index.blade.php
I want to call /admin0admin url to /resources/views/admin/index.blade.php.
I want to call /admin0admin/blog url to /resources/views/admin/blog/index.blade.php
Here how i call in view:
<a href="{{ route('admin0admin.blog') }}" class="br-menu-link">
And my routes like:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin0admin'], function () {
Route::get('/', 'HomeController#index')->name('index');
Route::group(['prefix' => 'blog'], function () {
Route::get('/', 'BlogController#index')->name('index');
});
});
And my BlogController index method:
return view('admin.blog.index');
I got an 404 not found error.
Route [admin0admin.blog] not defined
Laravel Version is : 5.6.*
You need to name the route admin0admin.blog, not index. prefix does not affect names of routes, so you need to write it out.

Laravel 5.2 authentication

I've been trying to get the new release (5.2) of Laravel to work with a simple web app. However, I'm having a problem with authentication.
All pages of the app include a navigation view partial which uses Auth::user()->name to display the username in the nav if they are logged in.
In order to do this, I created a pages controller which loads the auth middleware in the constructor:
public function __construct()
{
$this->middleware('auth');
}
This works perfectly if the user is logged in. However, if the user is not logged in, they are requested to login on every page. Even pages like "contact" or "about" which clearly should not require authentication to view.
How can I make pages like "about" always accessible while still being able to access Auth in the nav?
EDIT:
Routes
Route::group(['middleware' => ['web']], function () {
Route::get('/home', 'StaticController#home');
Route::get('/about', 'StaticController#about');
Route::get('/contact', 'StaticController#contact');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController#index');
});
StaticController
class StaticController extends Controller
{
public function home()
{
return view('static.home');
}
public function about()
{
return view('static.about');
}
public function contact()
{
return view('static.contact');
}
}
Navigation
<ul class="nav navbar-nav">
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li><i class="fa fa-btn fa-sign-out"></i>Logout</li>
</ul>
</li>
#endif
</ul>
After deleting everything and installing a fresh copy of Laravel the problem disappeared.
You have two possible solutions:
Route middleware (can get a bit hard to maintain) https://laravel.com/docs/master/middleware#assigning-middleware-to-routes
Within your view partial you could simply have a conditional statement to check or create a method on the Auth facade. This would mean you wouldn't need the conditional. Conditional solution below:
Auth::check() ? Auth::user()->name : ''
In /app/Http/Kernel.php
check to see if you have
'auth' => \App\Http\Middleware\Authenticate::class,
Example
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
....
];
Now, you may need to re-architect your route to something like this
in your routes.php
**//Routes (Not Require Log-in)**
Route::get('/home', 'StaticController#home');
Route::get('/about', 'StaticController#about');
Route::get('/contact', 'StaticController#contact');
**//Authentication Routes**
Route::group(['middleware' => ['auth']], function () {
Route::get('/', 'HomeController#index');
//................................
// More Auth Routes Go in HERE
//................................
});
Hope it helps !
you can do something like this
$this->middleware('auth', ['only' => 'update'])
the only will be set on the specified method for example

Method [delete] does not exist

I'm working in a laravel application and trying to implement delete() method in a controller but it doesn't work.
This is the Error:
BadMethodCallException in Controller.php line 283: Method [delete] does not exist.
Here is my view (buss.blade.php)
<a href="{!! URL::to('delete_bus', array($u->id)) !!}">
<span class="glyphicon glyphicon-minus"></span>
</a>
Here is my Route:
Route::get('delete_bus/{id}', array('uses' => 'adminController#delete'));
and it exists in app/resources/admin/buss.blade.php
And Here is my Controller Method
public function delete_bus($id) {
$reg = Business::find($id);
$reg->delete();
return Redirect::to('buss')->with('del', 'Sucessfully Deleted!');
}
You named your controller method delete_bus, but your trying to route to delete method. Change your route to this:
Route::get('delete_bus/{id}', array('uses' => 'adminController#delete_bus'));
And your controller should be called with capital letters, so this could be potential bug as well.

Laravel 4: Unable to generate a URL, as such route does not exist

Here is my routes.php:
Route::group(['prefix' => 'mine'], function () {
Route::get('/first', ['as' => 'mine.first', 'uses' => 'MyApp\Controllers\MyController#first']);
});
Here is my HTML/Twig file:
{{ form_open({'action': 'mine.first'}) }}
{{ form_submit('Start') }}
{{ form_close }}
And here is my controller:
class MyController extends BaseController {
public function first()
{
\View::make('stuff.mine.first'); //in folder app/views/stuff/mine
}
}
The error is "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "MyController#first" as such route does not exist.") in "stuff.show" at line 130."
All the answers on this topic that I've seen are to name the route, but I've already done that.
Also, when I go to the URL manually (localhost/mine/first), the screen is blank even though there is HTML in that file.
Any idea what's going on? Thanks.
mine.first is a route name, not an action.
Use:
{{ form_open({'route': 'mine.first'}) }}
As for the view, controller action need to return a Response (the View generates one), so you just need add the proper keyword:
public function first()
{
return \View::make('stuff.mine.first');
}

Laravel Controller method not found for homepage

I am aware that my code is slightly wrong (hence my post!). I am wanting my 'home' view to be displayed when a visitor accesses the '/' part of the website.
Currently, the view works when a user accesses the '/home' part of the website. I am currently pulling my hair out on how to do this!
Route.php:
Route::controller('/', 'HomeController');
Route::controller('users', 'UsersController');
Route::get('events/{id}/{slug}', 'EventsController#show');
Route::controller('events', 'EventsController');
HomeController.php:
<?php
class HomeController extends BaseController {
protected $layout = "layouts.main";
public function getHome(){
$events = myApp\Event::where('date','>=', DB::raw('CURDATE()'))->first();
$this->layout->content = View::make('home', array('events' => $events));
}
}
Home.blade.php:
<div class="col-md-4">
<h2>Next Event</h2>
<h3>{{$events->title}}</h3>
<p>Presented by {{ $events->consultant()->first()->title }} {{ $events->consultant()->first()->surname }}</p>
<b><p>{{ date("j F Y", strtotime($events->date)) }} from {{ date("g:ia", strtotime($events->start_time)) }}</p></b>
<a class="btn btn-success" href="{{ URL::to('events/' . $events->slug) }}">Book your place now.</a>
</div>
I have managed to get the view working with the '/' directory by using this within my routes.php:
Route::get('/', function(){
return View::make('home');
});
However, I am presented with the error:
Undefined variable: events (View:/Users/Sites/gp/app/views/home.blade.php).
It's as if, the HomeController isn't passing the 'events' array into the view, by just changing the route?! Any help/remedy/explanation would be hugely appreciated.
That's how Laravel RESTful controllers works, but you can create a new route for /, before your other routes, pointing to that action:
Route::controller('users', 'UsersController');
Route::get('events/{id}/{slug}', 'EventsController#show');
Route::controller('events', 'EventsController');
Route::get('/', 'HomeController#getHome');
Route::controller('/', 'HomeController');
EDIT
You have to understand that the Laravel Routing System tries to resolve a route as fast as it can, so if it finds a route that fits the current URI, it will use that route and forget about all the others. An example:
Route::get('/{variable}' 'Controller#action');
This is pretty generic route and can be resolved to anything, even
http://your-site.dev/events
So, if you add that route before this one:
Route::get('events/{id}/{slug}', 'EventsController#show');
Your events route will never be hit. That's why your most generic route have to be the last one.

Categories