How to route with id in URL - php

I want to route with the URL accept_form/1 which I get after clicking an anchor tag.
What should I write in my Route?
I am trying this:
Route::get('accept_form/{$id}', 'Controller#accept_form');
View :
<a href="{{ url('accept_form/' . $key->id) }}" class="small-box-footer">
More info <i class="fa fa-arrow-circle-right"></i>
</a>

Give a specific name of your routes like below.
Route::get('/accept_form/{id}', 'YourController#yourMethod')->name('accept_form');
Then pass parameters with array along with route function,
More info

Named your route,
Route::get('/accept_form/{id}', 'YourController#yourMethod')->name('accept_form');
Then You can use,
More info

you can do like this
{{ route('accept_form', ['id'=>$id])}}

Change Route to
Route::get('accept_form/{id}', 'Controller#accept_form');
Anchor tag
More Indo

Related

Passing a user id in a url in laravel

I'm making a Laravel web application, in this application, I have an Account Settings page. On this page, I want to display all the orders the user has made. In order to do this, I have to get the User id from the URL.
I tried passing the User id thru the URL by putting the Auth::user()->id in the route that leads to the page. however, this is giving me a 404 not found error.
This is the link that should pass the ID in the URL.
<a class="dropdown-item" href="{{ route('account/{{ Auth::user()->id }}') }}">
{{ __('Account Settings') }}
</a>
I also tried to change my web.php file but that's also not giving any result.
I really would appreciate some help with my problem since I've been stuck on this all day.
You're using Laravel route helper. the route would work only for NAMED ROUTE,
for example:
Route::get('account/{userId}', 'AccountController#show')->name('account');
Then, your could should be:
<a class="dropdown-item" href="{{ route('account', ['userId' => Auth::id()]) }}">
{{ __('Account Settings') }}
</a>

how to call a blade file dynamically in laravel?

I'm calling a blade that way:
<a href="{{ asset('views/quem-somos.blade.php') }}" class="dropdown-toggle"
data-toggle="dropdown">Quem somos</a>
but is returning that in the file does not exist, how would the correct way of calling in href be?
You shouldn't be calling the view files like that, create a url in the routes.php/web.php depending on your laravel version and have the url point to the blade instead.
Route::get("/quem-somos", function() {
return view("quem-somos");
});
And in the current file, change the link to
Quem somos
Use #include('quem-somos') wherever you're wanting the blade to be inserted.

Laravel blade: Unable to get id from object in url

i am working on eCommerce project where i got stuck with a issue. i am using url() to generate my url. i have a following code.
<a style="cursor: pointer;" href='{{ url("search?category=$categories[$i]->id")}}' class="menuLinks leftCategoriesProduct"> <span class="catText">{{($categories[$i]->name)}}</span></a>
on click i require
http://localhost/a2z/public/search?category=5
it worked on my one of the blade in the same project but here it generate a different url :
http://localhost/a2z/public/search?category={%22id%22:5,%22name%22:%22All%20Offers%22,%22slug%22:%22all-offers%22,%22user_id%22:2,%22parent_id%22:null,%22deleted%22:0,%22created_at%22:%222017-04-10%2013:21:48%22,%22updated_at%22:%222017-04-10%2013:21:48%22,%22childs%22:[]}-%3Eid
Here it returns the whole object however i need only the category id
I am not sure where i am doing wrong, is there a concatenation problem, or something related to blade which i am missing.
Help will be appreciated, Thanx
code:
{{ url('search') }}?category={{ $categories[$i]->id }}
I think you can try this:
<a style="cursor: pointer;" href="{{ url('search', $categories[$i]->id) }}" class="menuLinks leftCategoriesProduct"> <span class="catText">{{($categories[$i]->name)}}</span></a>
Hope this work for you !!!
<a style="cursor: pointer;" href="{{ url('search?category='.$categories[$i]->id)}}" class="menuLinks leftCategoriesProduct"> <span class="catText">{{($categories[$i]->name)}}</span></a>

Laravel 5 check if current url matches action

I am currently generating my application urls using {{action('Namespace\Class#method')}}. How would I check if the current page request maps to that current action Namespace\Class#method?
I would like to do something like:
<a href="{{action('Namespace\Class#method')}}
#if (currentAction('Namespace\Class#method'))
class="active"
#endif
>Some link</a>
How would I achieve this in Laravel 5?
There's no built in method for this, however you can retrieve the current action name with Route::currentRouteAction(). Unfortunately this method will return a fully namespaced class name. So you will get something like:
App\Http\Controllers\FooBarController#method
You can either check for that or use something like ends_with so you don't have to specify the full path:
#if(ends_with(Route::currentRouteAction(), 'FooBarController#method'))
You might also consider naming your routes with 'as' => 'route.name'. This would allow you to use: Route::is('route.name')
Actualy I had it simplified to the following code:
<li class="#if(Route::is('getLogin')) active #endif">Login</li>
That assumes that you have named routes. Which is a good idea in the first place, because you migth change the url of an action without going through your whole project to change the links to that action.
I know this is old, but for what it is worth.
Laravel has a couple of built-in helper methods for referring to URLs action and route.
action
Your route file would look like this.
Route::get('/funtastic', 'FuntasticController#show');
Your blade view would look like this
<a href="{{action('FuntasticController#show')}}
#if(action('FuntasticController#show') == Request::url())
class="active"
#endif
>Some link</a>
route
If you use named routes.
<a href="{{route('namedRoute')}}
#if(route('namedRoute') == Request::url())
class="active"
#endif
>Some link</a>
This is how I do it without any named routes
<a class="{{ str_contains(request()->url(), '/some-page') ? 'active' : '' }}" href="/some-page">Some Page</a>
This is not a perfect solution but it works for most cases
It works perfectly for me.
<a class="#if (\Route::current()->getName() == 'device_media') active #endif" href="{{ route('device_media', ['brand_slug'=>$brand->slug, 'slug'=>$device->slug]) }}" > Media</a>

how to link back to index.php in the fat free framework

I have created some routes with associated html links and they all work fine, except the homepage route (link).
So when i press on the home link to go back to index.php, it does not work.
I have already tried using a slash as key in my array, but that takes me too far back.
What can i put in the array so it links back to index.php?
here is the code in index.php
$f3=require('lib/base.php');
$f3->set('AUTOLOAD', 'model/');
$f3->set('UI','view/');
$f3->route('GET /', 'content->home');
$f3->route('GET /about','content->about');
$f3->route('GET /jobs','content->jobs');
$f3->set('menu',array(''=>'home','about'=>'about','jobs'=>'jobs'));
$f3->run();
here is the code in my template header.htm
<repeat group="{{ #menu }}" key="{{ #key }}" value="{{ #link }}">
<a href="{{#key}}" {{ #pagetitle==#link?' class="active" ':' ' }} >{{ #link }}</a>
</repeat>
The problem is not the php code. The issue is that your app runs in a sub-directory, that's why <a href="/"> brings you up to the webroot. Try to use an absolute link path here.
After taking a good look at the fat free framework documentation i found this:
$f3->get('BASE');
This is a ff global that gets you back to index.php
I've saved it in my own variable like so:
$f3->set('home',$f3->get('BASE'));
{{ #menu.top.home }}
<a class="{{ #view=='mainpage.html'? 'active ' : ''}}item" href="{{ #BASE }}/home">{{ #menu.top.home }}</a>
Localization example with URL link assembling

Categories