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>
Related
I am currently on
http://127.0.0.1:8000/cars/
I have a link that has to route me to
http://127.0.0.1:8000/cars/create
I used this in my code
<a
href="cars/create"
</a>
In my web.php I have following route
Route::get('cars/create',function ()
{
return view('carsops.create');
});
When I click on link I am redirected to
http://127.0.0.1:8000/cars/cars/create
Instead of
http://127.0.0.1:8000/cars/create
What is the error why I am getting this extra /cars.
Can some one help me.
You have to put a / in front of the link:
<a
href="/cars/create"
</a>
This means Go to the site root, then got to cars path then go to the create path.
This is solution is not nice. Why? Ok, you should use route names in the your application.
How to solve this problem correctly?
A first step is set name to your route. Modify routes/web.php like to
Route::get('cars/create',function ()
{
return view('carsops.create');
})->name('carsops.create');
Look at the name. Name can be as you wish.
Step 2 is, calling your route in the blade like to
<a
href="{{ route('carsops.create') }}"
</a>
Well done.
Laravel a href links are made by giving name,
if you want a return view without variables you don't need to get a method
Route::view('cars/create','carsops.create')->name('yournamelink');
use in blade page
goto page
A root-relative URL starts allway with a / character, to look something like create car.
The link you posted: create car is linking to an folder located in a directory named create in the cars, the directory cars being in the same directory as the html page in which this link appears.
It is therefore easier to work with Laravel Helper. As already mentioned here.
<a href="{{ route('car.create') }}"</a>
You define the route names in your web.php and for api links in your api.php file.
Route::get('/create-car', [App\Http\Controllers\PageController::class, 'create'])->name('car.create');
have the following problem, and is that I need to redirect from a link which is created by me with 6 random characters to make a shortener to a file. The problem is that when I try to access the file I get the url http://127.0.0.1:8000/C:/Users/myUser/Desktop/proyect/public/assets/PDFs/file.pdf but I do not need this url, but the following, which would be from C://.
My doubt is, how could I do this? Since I can't find anything that can solve it and I have been trying for days to find the problem and trying in other ways, even with:
Storage::disk('local')->get($qr->document)
The blade code is as follows:
<a class="button shortlink" href="{{ route('shorten.linkDocument', $qr->code) }}" target="_blank"> <i class="fa fa-link"></i>
{{ route('shorten.linkDocument', $qr->code) }} </a> <br>.
Where it says $qr->code are the 6 random characters generated to act as a shortener and redirector. The route to shorten.linkDocument is found in Web, and is as follows:
Route::get('Pdf/{code}', '\App\Http\Controllers\QrController#shortenLinkDocument')->name('shorten.linkDocument');
The method referenced by the controller is as follows:
public function shortenLinkDocument($code).
{
$find = Qr::where('code', $code)->first();
return redirect($find->document);
}
PS: In $qr->document the absolute path from C:// is stored.
I hope for your help and thank you very much if you have read everything.
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
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>
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