How to get a laravel route group to work - php

I am trying to set up a Laravel route group in Laravel 5.5 and use it in a blade. However I am getting the a Route not defined error. The full error is :
"Route [admin/route_group_test] not defined. (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php)
I have looked through the documentation and it looks like I am doing it right. Here is the line from the route file:
Route::prefix('admin')->group(function(){
Route::get('route_group_test','AdminController#testingMiddleWare');
});
and the link from the blade:
{{route('admin/route_group_test')}}
I have no idea what I am doing wrong

The route() helper uses route's name. From the docs:
The route function generates a URL for the given named route
So you need to name the route:
Route::get('route_group_test', 'AdminController#testingMiddleWare')->name('admin.route_group_test');
Or:
Route::get('route_group_test', ['as' => 'admin.route_group_test', 'uses' => 'AdminController#testingMiddleWare']);
And then use it:
{{ route('admin.route_group_test') }}
Or you can use unnamed route:
{{ url('admin/route_group_test') }}

Try the below code:
Route::group(['prefix'=>'admin','namespace'=>''], function () {
Route::get('route_group_test','AdminController#testingMiddleWare');
});

Related

Route [controller.create] is not defined when I've already defined the Resource Controller in my Web.php

So I'm working with two Resource Controllers (I'm not sure if that's the problem but if it is, please let me know); one of them is StudentController and its URL is "/main". It's working as it should, perfectly. Now I'm creating the 2nd Resource Controller which name is TeacherController, and its URL is "teacher/main" - i.e its files are in the 'teacher' folder. This is my web routes file:
Web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/main','StudentController');
Route::get('profile','ProfileController#index')->name('profile');
Route::get('profile/edit','ProfileController#edit')->name('profile/edit');
Route::put('profile/edit/{id}','ProfileController#update')->name('profile.update');
Route::resource('teacher/main','TeacherController');
Now when I go to teacher/main - it says that "teacher.main.create" is not defined. That is the route that I've used on my create button i.e
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
And this is the create() function in my Resource Controller:
public function create()
{
return view ('teacher/create');
}
From my understanding of Laravel Resource Controllers, I shouldn't have to define each Resource Controller function individually as Laravel should automatically detect each of them since I've already defined Route::resource('teacher/main','TeacherController'); in my Web Routes. What am I doing wrong here? This is just the first function it's getting, then there are other functions like Edit/Destroy that will also pop-up route errors and I don't want to go through with defining each of them individually. Help is highly appreciated.
Try to use in group with prefix and as
Route::group(['prefix' => 'teacher','as'=>'teacher.'], function () {
Route::resource('main','TeacherController');
});
Now you can use teacher.main.create
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
I just tested this locally, and seems like you're using wrong name for your resource route. I set the routes exactly as in your example, and this is what I got:
So if you wanted to use the create route, you'd do it like this:
{{ link_to_route('main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
On the other hand, if you want to keep the names you wanted, you can set a custom name for each of your resource route, like this:
Route::resource('teacher/main', 'TeacherController', [
'names' => [
'index' => 'teacher.main.index',
'create' => 'teacher.main.create',
// etc...
]
]);
Hope this helps. Happy coding.
Route::resource('teacher-main','TeacherController');
now you can access
teacher-main.index

Laravel basic view returning route does not work

I have this problem when I try to access the page vie link it works properly. But when I try to write the route inside a link in order to access it using navbar, it throws the following Error :
Route [home] not defined. (View: C:\wamp64\www\projectName\resources\views\layouts\master.blade.php) (View: C:\wamp64\www\projectName\resources\views\layouts\master.blade.php)
And my route is as following:
Route::get('/home', function () {
return view('home');
});
This is how I write the route inside the navbar link:
{{ Route('home') }}
Any help is highly appreciated.
When you use named route you have to give name to that particular route. change your code to:
Route::get('/home', function () {
return view('home');
})->name('home);

how to use named routes in view where named routes have wildcard

I've a group routes like this
Route::group(['prefix' => 'admin/{username}'],function()
{
Route::get('', ['as' => 'dashboard', 'uses' => 'AdminController#index']);
Route::get('settings', ['as' => 'settings', 'uses' => 'AdminController#settings']);
});
In my view i used named routes for link reference like this
<li><span>Settings</span></li>
but it's rendering as
<li><span>Settings</span></li>
the username is printing literally but not the actual value. What i really want is this
<li><span>Settings</span></li>
How to do this?
You should try following...
<li><span>Settings</span></li>
You would have to pass current username as second parameter.
You have to pass route parameters when generating an URL. Like:
{{ route('settings', ['john']) }}
Obviously you would rather do something like:
{{ route('settings', [$user->name]) }}
Since you only have one parameter you don't even have to pass it as array:
{{ route('settings', $user->name) }}
And thus the address looks better you can use strtolower() if you have a username with a capital letter.
<li><span>Settings</span></li>
Caution
Auth::user() is for the current user. If no one is logged in you get an error!
So you can do a check.
#if(Auth::user())
<li><span>Settings</span></li>
#endif
Another way to do that is passing the wild card to view via controllers and use it in route as second parameter
first pass the wildcard in controller
public function index($username)
{
return view('admin.dashboard', compact('username'));
}
and in view pass the variable in routes as second parameter
<li><span>Settings</span></li>

Custom method instead of resource for Laravel routes

Using 4.2 and trying to add a custom method to my controller.
My routes are:
Route::get('ticket/close_ticket/{id}', 'TicketController#close_ticket');
Route::resource('ticket', 'TicketController');
Everything CRUD wise works as it should, but at the bottom of my TicketController I have this basic function:
public function close_ticket($id) {
return "saved - closed";
}
When I am showing a link to route on my page:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
I constantly get a route not defined error, but it surely is defined...?
Any ideas where this is going wrong?
link_to_route expects a route name, not a url. This is why you are getting 'route not defined' errors, because you have not defined a route with the name you supplied to link_to_route. If you give your route a name, you can use link_to_route.
Given the following route definition, the name of the route is now 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController#close_ticket'));
The value for the 'as' key is the route name. This is the value to use in link_to_route:
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
The laravel helper method link_to_route generates an HTML link. Which mean when clicked, the user will be performing a GET request.
In your routes file, you are defining this as a POST route.
Route::post(...)
Also, take a look at the docs for link_to_route here:
http://laravel.com/docs/4.2/helpers
You'll see that the first argument should be just the route name, without the ID appended.

Breadcrumbs error in Laravel

I'm trying to use the breadcrumbs by davejamesmiller
In the breadcrumbs.php file which is in the same directory as of route.php I have setup up this:
<?php
Breadcrumbs::register('courses', function($breadcrumbs) {
$breadcrumbs->push('Courses', route('courses')); });
and in the route.php I have:
Route::get('/courses', 'CoursesController#index');
and in the courses.index I called the breadcrumbs like this:
{{ Breadcrumbs::render('courses') }}
But I'm getting an error as follows:
Route [courses] not defined. (View: C:\wamp\www\lc2\laravel\app\views\courses\index.blade.php)
What might be the problem? I cant seem to figure out. I already have the route set for the courses.
i think you have to use laravel named routes - documentation , also in github repo readme
Route::get('/courses', ['uses' => 'CoursesController#index', 'as' => 'courses']);

Categories