Passing a user id in a url in laravel - php

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>

Related

Laravel redirect issue for android(kodular)

I'm building the mobile application of my laravel project for the profile part {{ __('main.nav_profile') }} This is how I do the redirect. But android button redirect asks for URL and so redirection fails. How can I solve this.
Picture : https://www.hizliresim.com/ml27x74
For example, there is no problem with others.
site.com/favorites and site.com/settings/edit
{{ __('main.nav_saved') }}
<div class="dropdown-divider"></div>
{{ __('main.nav_account') }}

How to route with id in URL

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

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

Is it possible to reload a page in Symfony2 with the get parameters intact?

In my web app if a user is logged in and clicks on the logo, instead of going to the entry page as an anonymous user, I would load their member homepage URL instead.
How it works is when a user joins or logs in, I generate this route $this->generateUrl('member_page'), array('member' => $member->getName()));
and my route is configured as so,
member_page: pattern: /member/{member}/
defaults: { _controller: HomeBundle:Default:member }
and it generates this url:
http://website.com/member/John+joe/
The problem is, when I recall the route it just shows this url
http://website.com/member/
I have tried calling a new action and using $request->getURI() but it does not maintain the dynamic get parameters.
I'm trying to avoid a call to the DB to get the user name each time the click the logo.
Any help would be great, thanks!
A few weeks ago, I had to do something quite similar and inspired myself from the PagerFantaBundle. Have you tried this code in your controller:
$request = $this->getRequest();
return $this->redirect($this->generateUrl($request->get('_route'), $request->query->all()));
Instead of using the username you could use the user ID which will be saved into the users session after log in. This will also avoid any conflicts with users who have the same name.
One of possible solutions would be to generate proper routes right in Twig:
{% if app.user %}
<a href="{{ path('member_page', {'member': app.user.name}) }}">
{% else %}
<a href="{{ path('your_homepage') }}">
{% endif %}
<img src="yourlogo.png" />
</a>

Categories