how to remove question mark form url in laravel
i use laravel 5.4 and i get this trey to use query in the link but it don't work
http://localhost/shooping-cart/public/add-to-cart/id?1
i want to remove id and question mark form url
i use laravel 5.4
Route::get('/add-to-cart/{id}', [
'uses' => 'ProductController#getAddToCart',
'as' => 'product.addToCart'
]);
Looks like the problem is in the way you generate the link. For this route you should use route() helper like this:
{{ route('product.addToCart', $productId) }}
Or create a link manually:
{{ url('add-to-cart/'.$productId) }}
Use ? mark after id like {id?}
And in the view file, generate link like this..
Link Name
It will definitely work.
Route::get('/add-to-cart/{id?}', [
'uses' => 'ProductController#getAddToCart',
'as' => 'product.addToCart'
]);
{{ route('product.addToCart', $productId) }}
I think this is just the way Laravel constructs the URL. Its more of an aesthetic than functional choice. If you do not wish to see a question mark in your url when performing GET request then use a named route with parameters.
View
If you access your controller by matching URL thru the router class.
<form method="get" action="/questions/{{$task->id}}">
<button type="submit" class="btn btn-info btn-sm">View</button>
</form>
You will see a question mark "?" appended to the end of your URL.
Related
I thought to write static routes like contact, imprint or "About Us" in the web.php as a one-liner. I saw this at Laravel Daily.
web.php
Route::get('/{page}', App\Http\Controllers\StaticPageController::class)
->name('page')
->where('page', 'about-us|imprint|contact');
It's nice but I'm getting problems with my navbar.
My Blade navbar has a dynamic part. The current menu item is highlighted. Very simple.
nav.blade.php
<x-nav-link :href="route('about-us')" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
With the new one-liner, I then get the following error message:
Symfony\Component\Routing\Exception\RouteNotFoundException.
Route [about-us] not defined. (View: /project01/resources/views/includes/nav.blade.php)
Which is logical, because I no longer have the about-us route.
Actually, the route should be given a corresponding array mapping. But I don't know how. How can I solve the problem?
The solution for this problem will be:
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
Special Thanks for all good answears!
You have two options.
1- Change the routeIs() to is() and use the path, not the route name
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->is('about-us/*')">
{{ __('About us') }}
</x-nav-link>
2- Switch back to 3 routes instead of the one liner. There is no gain in using the one liner. It is slower and harder to read (maintain)
You have assigned a name page to a given route with the required parameter named page. So, you must use the following code:
route('page', ['page' => 'about-us']);
https://laravel.com/docs/6.x/routing#named-routes
I think there is a misunderstanding.'about-us|imprint|contact' is the parameter restrictions.'page' is the route name.So this should work;
<x-nav-link :href="route('page','about-us')" :active="Request::url() == route('page','about-us')">
{{ __('About us') }}
</x-nav-link>
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
I got a question using laravel, I am trying to call a function and passing some parameters along with it using the {{ route }} method something along these lines here:
<img src="{{route('cacheImage', ['general', 'mini-logo.png']) }}" />
My route in my web.php file looks like this:
Route::get('/cache/images', ['uses'=>'HomeController#cache_image','as'=>'cacheImage']);
As a result, I am getting something like this here when the page loads in my img src:
http://localhost:8000/cache/images?general&mini-logo.png
But this is not what I want, what I would like is to have something along these lines here in my img src:
http://localhost:8000/cache/images/general/mini-logo.png
At the same time I want to call the cacheImage function in my controller and pass it both parameters, which are "general", and "mini-logo.png"
How do I achieve this? Can someone give me an example?
Change your route to this.
Route::get('/cache/images/{type}/{name}', [
'uses' => 'HomeController#cache_image',
'as' => 'cacheImage'
]);
Controller method to this to be able to receive the url segments as parameters.
public function cache_image($type, $name)
{
dd($type, $name);
}
Then you can generate links like so.
$link = route('cacheImage', ['general', 'mini-logo.png']);
// generates http://example.dev/cache/images/general/mini-logo.png
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>
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.