Laravel 4 Url Helper functions not matching protocol - php

I had read somewhere the the Laravel helper functions route() and url() would match the protocol of the current page. This has not been my experience and I cannot find a way to do it cleanly.
<!-- https://domain.com/public/route1 -->
<a href='{{ route('route2') }}'> domain.com/route2 </a>
404 due to lack of "/public/" and no https
<a href='{{ url('/route2') }}'> domain.com/public/route2 </a>
proper path but still drops https
<a href='{{ secure_url('/route2') }}> https://domain.com/public/route2 </a>
proper path and protocol but is explicit
Is there a way to do what I want? It seems absurd to me that these helpers would be unable to accomplish something this simple. Something that the following line of code does with no tricks (its downside being the obvious loss of benefits associated with using helper functions [UPDATE: in particular having everything break when running locally due to a different app path]):
<a href='/public/route2'> https://domain.com/public/route2 </a>
To further convolute things I would like to add that when running on localhost, the first version {{ route('route2') }} produces a path that includes /public/, though it is possible that I have a different config file somewhere on the server (this is a very large project). Bonus points if someone can point me in the right direction for that issue as well.
routes.php:
Route::group(array('prefix' => 'route2' ), function(){
Route::get('/', array(
'as' => 'route2',
function(){
return View::make('pages.route2');
}
));
...
});
Thank you kind souls!
UPDATE:
I have continued searching and been unable to find any relevant information regarding Helper function url generation. It seems the majority of info with similar keywords is about forcing HTTPS site-wide (server config) or setting up redirects. It just seems so much simpler to generate the correct link in the first place. Plain HTML can do it, so should Laravel.
UPDATE 2:
Using the asset helper seems to work how I would expect url to. Please someone tell me this hack is not my only option...

Related

Why I am getting wrong URI?

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');

Change page and URL without refreshing the Page

Hi and I'm terribly sorry for asking such question.
I'm quite catching up on Laravel, maybe 30% smarter than before but I'm stuck on this kind of scenario.
How do I call a blade with all parameters passed to its controller without refreshing the page.
Here's my folder structure (I will not place all just controller, pages)
Folrder Structure
Now, I'm in let's say dashboard.blade.php
In the Side bar of my dashboard I want to go to a link
<li class="nav-item">
<a href="{{url('/get-greeting/{id}/{user}')}}" class="nav-link get-greeting">
<i class="nav-icon fas fa-table"></i>
<p>
Requested Son
<span class="right badge badge-danger side-span requested-span"></span>
</p>
</a>
</li>
Route will be
Route::any('/get-greeting/{id}/{user}', 'DefaultController#pullGreeting')->name('getgreeting');
Controller will be
public function pullGreeting($id, $user){
$userName = User::activeUser($user);
$defaultarray = array( 'greetingLogs' => recordLogs::pullgreetings($user) );
return view('pages/department1/greetings',$defaultarray);
}
When I click it, it refreshes the page. Anyone, can you help me and pinpoint where am I doing wrong so I can make it not refreshing the page but instead changing the blade dynamically?
Does this matter?
return view('pages/department1/greetings',$defaultarray);
or
return view('pages.department1.greetings',$defaultarray);
PHP is a server-side programming language therefore any changes to the view will require a full page refresh.
Without the use of Javascript, achieving a part page refresh would require the use of an old school technique called Frames.
You could redesign your application to render HTML via different endpoints and have a Javascript powered frontend that calls to those endpoints.
You could easily achieve through the use of JS Libraries such as React or JQuery.

Laravel 5.5: Blade forces https on external URLs

i'm displaying external Website Links from my Users in a Profile Page. The URL Format is: www.xyz.com. When i put a http:// before the {{ url }} Laravel changes the Protocoll automatically to https://. In the Result a lot of the Websites don't open properly. I set the APP_URL in the .env File to
https://domain because i want to use SSL in the App.
Laravel (Blade) is removing the http:// from the URL when i just want to print the Variable too. Even when i put http:// right in front the URL in the DB (MySQL) i still have the same problem.
All i do in the Controller is:
$user = \App\User::find($id);
After 2 days of research i'm reaching out to the community. Basically i'm searching for something like secure_url() inverse.
What i tried:
{{ url('http://'.$url) }}
{!! url('http://'.$url) !!}
{!! url(e('http://'.$url)) !!}
<a href="http://{{ $url }}">
{{ $url }}
Putting the Protocoll right in the DB
What do i not get here? Thank you for any help.
EDIT
Laravel does this only when i visit my site over https.
UPDATE & SOLUTION
It was the problem of the Laravel Cache Speed Package. I deactivated it and everything was back to normal. My fault, my bad.

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>

Laravel "{slug}" not being replaced, instead being url encoded

I've recently embarked on learning Laravel. In doing so I've began completing tutorials from around the web, including from youtube. In one such tutorial I've been developing a very basic blog. I've hit something of an impasse when working with slugs. For some reason, it just doesn't want to work for me!
When the following code is entered in my 'routes.php' file, I have noticed that the braces '{' and '}' are automatically converted to URL encoding and the word "slug" is displayed. Additionally, the '/' following the word posts is nowhere to be seen. This means a link will read as "http:[domain name]/posts/%7Bslug%7D[article name]"
Route::get('/posts/{slug}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The problem is partially resolved if I change the code thus:
Route::get('/posts/{slug?}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The word "slug" is no longer displayed as part of the URL. However, for some reason the slug's preceding '/' is still not visible, as though eaten by the slug in question! It now reads "http:[domain name]/posts**%7Bslug%7D**[article name]".
I'd really appreciate pointing in the right direction if anyone can lend a hand. Thank you.
Based on your comment, the way you're calling the action is incorrect. Try this view code:
#if ($posts->count())
#foreach ($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
{{ Markdown::parse(Str::limit($post->body, 157)) }}
read moreā€¦
</article>
#endforeach
#endif
I am not near a terminal to test this, but it should get you closer. The problems this code addresses are:
To generate a URL using the route name, use route. Your code was using action.
To pass a parameter and fill in the "{slug}", you pass an array as the second argument to route (and action for that matter). Your code was neither passing an array nor passing the value into the function.
You were missing a } after the Markdown::parse, but that may have been a copy & paste error.
If this helped you, remember to up vote and mark as accepted!

Categories