I got an error : Route [admin/news] not defined I try to use #component in my create.blade.php
In my Controller I declare variable
public $route = 'admin/news' ;
In web.php
Route::post('admin/news/create', 'Admin\NewsController#store');
Route::resource('admin/news', 'Admin\NewsController');
In my html this return right Url
<a class="btn btn-success" href="{{ asset($route.'/create') }}">add</a>
<a class="btn btn-success" href="{{ route('news.create') }}">add</a>
I check my route by using php artisan r:l
It has a news.create
I try to use other routes both of these work fine not sure what's wrong with my create route
route('news.edit',$t->id)
route('news.destroy',$t->id)
the problem is in my create.blade.php I try to use #component by this
#component('layouts.submitform',
['id'=>'create','method'=> isset($edit) ? 'PUT' : 'POST' ,'action'=> isset($data->id) ? asset($route.'/'.$data->id) : route($route)]
)
You have $route set to admin/news. You say you want to go to the create page. You then say that the route is named news.create. So use news.create as the name when referencing it with the helper. Set $route to news.create.
You seem to want to use a URI and a route name. You have to decide which one you are going for.
Laravel Docs - Routing - Named Routes
Laravel Docs - Helpers - Url Helpers - route
Related
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
After reading the documentation, I still only have a vague idea of what Named Routes are in Laravel.
Could you help me understand?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController#show')->name('profile');
It says:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
I don't understand what the second part of the sentence means, about generating URLs or redirects.
What would be a generated URL in the case of profile from the above example? How would I use it?
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route.
But consider you used a named route
Route::post('/task', 'TaskController#store')->name('task.store');
With named routes you can use the route like this in your view:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need.
Route::post('/task/now/go/here', 'TaskController#store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating.
After adding a name to a route, you can use the route() helper to create urls.
This can now be used in your application.
For instance, in your blade templates this may look like:
{{ route('profile') }}
This will use the application url and the route path to create a url.
this is how it looks it:
named route sample name('store');:
Route::get('/store-record','YourController#function')->name('store');
store is the named route here. to call it use route('store')
defining another type of route. this is not named route:
Route::get('/store-record','YourController#function')
you can access this route using {{ url('/store-record') }}
hope this helps
I want to show a link if the route is not the current route, however I'm getting an error when I try to include the link.
In my header blade:
<a class="{{ Route::is('start') ? 'active' : '' }}"
href="{{ URL::route('start') }}">Start</a>
In my web.php:
Route::get('/start', 'Start');
I'm getting the error:
Route [start] not defined. (View: /var/app/current/resources/views/include/header.blade.php)
My start route works if I go to myurl/start, but when I add it to my header it throws the error. I ultimately want to only display the link on non-start route pages.
You probably have to apply a "named route" for your route. I'm leaving you a link here below:
Named Routes in Laravel 5.5
In your route above this would become Route::get('/start', 'Start')->name('start')'
Notice the ->name('start') at the end. Then you'll be able to reference the route just by its name.
I think the error caused by the difference of the first character between "start" and "Start".
In web.php, you use 'Start' while in blade file is 'start'. Please correct it and check again.
Hope it works well
I installed a Git laravel (employee-mgmt-laravel5.4-adminlte)
Now I'm trying to export an invoice to excel but the route doesn't work and I'm getting this error : Route [facture-management.export] not defined.
This my button
<a class="btn btn-primary" href="{{ route('facture-management.create') }}">Ajouter factures</a> (this one work)
<a class="btn btn-primary" href="{{ route('facture-management.export') }}">excel</a> (this one is not working)
This is my route
Route::resource('facture-management', 'FactureManagementController');
Route::post('facture-management/search', 'FactureManagementController#search')->name('facture-management.search');
What is going wrong because .create and .update are working but why is .excel not working ?
In your FactureManagementController you should have an export method.
and then: Route::get('your-url', 'FactureManagementController#export');
Because you have a resource route, you have the index/create/show/edit/update/destroy methods, but export you have to create.
If you do have it defined, please post your controller.
I can see that you are using a laravel controller resource. Laravel controller resource comes with the create and update function by default. You are getting this error because facture-management#export is not defined in your controller. Check your facture-management controller and see if public function export() exists. If not, you need to create one and define its functions.
I might be misunderstanding the link to route helper, but its not working without a route set up in my routes file.
{{ link_to_action('UserController#loginWithFacebook', 'Facebook Login in', $parameters = array(), $attributes = array('class' => 'btn btn-primary fb-login-btn')); }}
Then an old route when I was linking to a URI was:
Route::get('loginuser2', array('uses' => 'UserController#loginWithFacebook'));
However, I thought link_to_action was a direct call to the method. After removing the above link in my routes file I get not defined route errors for the controller method.
Any ideas how to avoid this?
You cant link to an action if the route itself does not exist. The route must be defined.
So you need to keep the route defined, and then link_to_action() will continue to work. On the backend it is looking at your routes to find the same route with that action - and uses that URL.
There is no way to avoid it.
link_to_action is used to link to a controller. you should use
link_to to generate html link
echo link_to('foo/bar', $title, $attributes = array(), $secure = null);