Laravel 5.2- How to add prefix to existing url() helper function?
Asked Answered
C

4

6

I use localization functionality in my web application. But I want to add App::getLocale() to url(). So for example, in my view when I add <a href="{{url('/admin')}}">link</a> I want to display the URL in HTML as http://localhost/mysite/en/admin. How can I do it?

Can I customize built-in URL helper function?

Cyanide answered 10/5, 2016 at 12:2 Comment(2)
I have the same problem right now, can you please tell me how you have solved this?Shed
@Shed , I found solution. Check my answer.Cyanide
C
-5

enter image description hereI found solution. Tested it in laravel 5.3. It worked! Could be useful for others. So it is worth share:

Enter vendor\laravel\framework\src\Illuminate\Foundation\helpers.php and modify code like that:

Just add your prefix before $path variable

Cyanide answered 2/9, 2016 at 11:52 Comment(3)
Never edit a class in vendor folder. This folder is entirely managed by composer. Any modification in it will be erased when you will run composer update.Ledger
Alexandre is right, don't do that. Never edit anything inside of the vendor folder. As you can see from the screenshot, the function url() is defined only if it's not defined already. You could define your custom url() function inside of an helper file located in your project folder and then put it in the autoload section of your composer.lock.Eccentric
Never ever do that. It will break the synchronization with production and your development code.Taligrade
C
2

In Laravel 5.4, use the following

{{ url('Your_Prefix', 'login') }}

url just formats the whole path to the requested address, you can add whatever you want in it.

Cenacle answered 4/3, 2017 at 21:19 Comment(0)
C
1

url() method doesn't allow that, as it generates URL for the exact value that you provide as first argument. However it's possible to achieve what you need if you switched to route() method and defined a prefix for your routes.

// define a route group with a prefix in routes.php
Route::group(['prefix' => App::getLocale()], function() {
  Route::get('admin', ['as' => 'admin', 'uses' => 'AdminController@action']);
});

// generate prefixed URL
echo route('admin');

If your locale is en, then the above line should give you a URL like /en/admin

Corbel answered 10/5, 2016 at 12:22 Comment(0)
G
0

I know this is an old question, but I ran into the same problem today. Based on the other answers, I came up with the following solution: I created a helper class which uses the Laravel url() helper function, but pre-processes the $path variable prior to calling the helper function. I created my own implementation for both url() and secure_url() helper functions.

This was a post action after I implemented localization in my application, for which I consulted this question.

Take a look at my solution. I hope it is useful for someone someday:

<?php

namespace App\Http\Helpers;

class Helpers {

    /**
     * Generate a url for the application.
     *
     * @param  string  $path
     * @param  mixed   $parameters
     * @param  bool    $secure
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
     */
    public static function url($path = null, $parameters = [], $secure = null) {
        $path = (string) $path;
        if (strlen($path) > 0 && $path[0] !== '/') {
            $path = '/' . $path;
        }
        return url(app()->getLocale() . $path, $parameters, $secure);
    }

    /**
     * Generate a HTTPS url for the application.
     *
     * @param  string  $path
     * @param  mixed   $parameters
     * @return string
     */
    public static function secure_url($path, $parameters = []) {
        return static::url($path, $parameters, true);
    }

}
Glaucescent answered 2/11, 2018 at 20:24 Comment(0)
C
-5

enter image description hereI found solution. Tested it in laravel 5.3. It worked! Could be useful for others. So it is worth share:

Enter vendor\laravel\framework\src\Illuminate\Foundation\helpers.php and modify code like that:

Just add your prefix before $path variable

Cyanide answered 2/9, 2016 at 11:52 Comment(3)
Never edit a class in vendor folder. This folder is entirely managed by composer. Any modification in it will be erased when you will run composer update.Ledger
Alexandre is right, don't do that. Never edit anything inside of the vendor folder. As you can see from the screenshot, the function url() is defined only if it's not defined already. You could define your custom url() function inside of an helper file located in your project folder and then put it in the autoload section of your composer.lock.Eccentric
Never ever do that. It will break the synchronization with production and your development code.Taligrade

© 2022 - 2024 — McMap. All rights reserved.