Create laravel link based on route - php

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

Related

Laravel Route not defined error in resource controller

I have route file defined as follows...
Route::get('pincodes/export', 'PincodeController#export'); Route::resource('pincodes','PincodeController');
I have a controller called 'PincodeController' where the default functions works without any issue. I wanted to add a new function called 'export' in this controller. I tried {{ route('pincodes.export') }} in my blade file to generate link to this page... and it throws the following error...
Facade\Ignition\Exceptions\ViewException Route [pincodes.export] not defined. (View: /Users/tst/Desktop/www/laravel/project/resources/views/pincode/index.blade.php)
but if i access the url directly in browser it works fine. why is that ?
You need to specifically name the route:
Route::get('pincodes/export', 'PincodeController#export')->name('pincodes.export');
If you are using Laravel 5.5+, change export route like this
Route::get('pincodes/export', 'PincodeController#export')->name('export');
use route function in your blade file like
route('export')

Passing parameter using routes laravel 5.8

I have two routes
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
but when i access the second routes, it always gets the first routes why?
and how to fix it?
thanks for helping me
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->where(['fasilitas_id' => '[0-9]+', 'any' => '[0-9]+']);
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->where('kf_id' => '[0-9]+');
For more info Regular Expression Constraints
Another way to pass it define name route.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('fasilitas.example1');
<a href="{{ route('fasilitas.example1',['fasilitas_id'=>1,'any'=>2]) }}">
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('fasilitas.example2');
<a href="{{ route('fasilitas.example2',['kf_id'=>1]) }}">
When hitting /fasilitas/get_kategori you trigger the first route, with get_kategori being the {fasilitas_id}.
Change the order of your routes, so /fasilitas/get_kategori gets triggered first:
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
The second segment of the first route is a wildcard, that means it could be anything.
When the second route is called in browser the second segment (/get_kategori/) is passing through the wildcard of the first route.
Changing the route order may solve the problem. But the best practice is changing the route name. Example:
Route::get('/fasilitas/something_else/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Try with name . It will be more efficient
Fasilitas !
May be you tried to write facilities.
Whatever, am writing base on ur method.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('detail.fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('get.kategori');
How to call at front page for two parameter and single parater ?
Have a look below .
For detail.fasilitas
Detail Fasilitas
For Single Parameter : get.kategori
Get Kategori
Try it.
Let me know its work or not .
Have fun with code.

Laravel Route goes to Controller but does not display data

I created the following route
Route::resource('posts','PostController');
Now I'm trying to create a custom route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
When the link posts/trashed is clicked, it goes to the PostController#trashed, but the controller shows blank page without any errors.
So changed the custom route to
`Route::get('post/trashed','PostController#trashed')->name('posts.trashed');
by simply changing the posts/trashed to post/trashed and the controller works fine.
Can someone explain what the issue is.
Figured it out. Solution was hidden deep inside the Laravel Documentation . https://laravel.com/docs/5.0/controllers#restful-resource-controllers
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:
Re-ordering will solve the issue.
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Route::resource('posts','PostController');
The resource route has some routes inside it own.
So this route:
Route::resource('posts','PostController');
contains this route :
Route::get('posts/{post_id}',PostController#show)
So it has conflict with your route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Solution 1:
bring your route upper than resource
I mean:
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed'); <-- first
Route::resource('posts','PostController');
Solution 2:
As you mentioned use another url (like post/trushed)
Solution 3 :
Add exception for resource.
Route::resource('posts', 'PostController')->except([
'show'
]);

Route not defined error in laravel , even though route is defined

I have the followning method in my controller:
public function showQualityResult($qualityData) {
return $qualityData;
}
When clicked on a link , i want that method to be invoked , so i have the following in my view file:
Submited Quality Check
Also, I have the following route setup:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
But having the below line of code:
Submited Quality Check
Does't really work , i get the following error in the frontEnd:
Now how can i solve this problem , and why am i getting this error of Route not defined , even though i have the route defined ??
route() helper uses route name to build URL, so you need to use it like this:
route('quality-result.show', session('quality-data'));
And set a name for the route:
Route::get('/showQualityResult', ['as' => 'quality-result.show', 'uses' => 'QualityCheckController#showQualityResult']);
Or:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')->name('quality-result.show');
The route function generates a URL for the given named route
https://laravel.com/docs/5.3/routing#named-routes
If you don't want to use route names, use url() instead of route()
The route() helper looks for a named route, not the path.
https://laravel.com/docs/5.3/routing#named-routes
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')
->name('showQualityResult');
Should fix the issue for you.
In my case I had simply made a stupid mistake.
Further down in my code I had another named route (properly named with a unique name) but an identical path to the one Laravel told me it couldn't find.
A quick update to the path fixed it.

Diffrence between Laravel Routing Method

what is the difference between both routing ? can anyone explain ?
Route::get('login', 'webcontroller#login');
Route::get('login', array('as' => 'login','uses'=>'webcontroller#login'));
Well. There is the flexibility of Route object (i think it belongs to Symfony)
In the first statement, you explicitly say that you what controller's action a certain address should trigger (in your case it is 'login' which triggers login() of WebController).
In the second statement, you can add an "array" of settings for the controller's method, which, in your case you have specified a name. "login", which is the name of your Route::get() rule for the address "/login", could be used any where in the system without you explicitly specifying any controller or url which gives you the ability to change whatever you like in the future, as long as you are consistent with your names.
You set a route:
Route::get("login", array('as'=>'login', 'uses'=>'LoginController#Login');
Then you can use it like:
$url = URL::route('profile');
Whil you are still able to change the url of your route:
Route::get("user/login", ...);
Without the need to change its uses of "name" within your project.
You can read about it on Laravel's official documentation:
http://laravel.com/docs/4.2/routing#named-routes
In the number 2, you usea an alias , is easy for call de route in the code:
example:
<a href=" {{ route('user.list') }} ">
< span class="glyphicons glyphicons-link"></span>
<span class="sidebar-title">Link</span>
</a>

Categories