Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

php - Creating a LaravelLocalization connection with a model

I am trying to use mcamara/laravel-localization library for Laravel 7. I have routes in my view files in the form of routes("admin.pages.show", $page) .

There are also language-specific URLs that I have defined in the langs/tr/routes.php file:

return [
    // Pages' Routes
    'pages.store'       => 'sayfa',
    'pages.index'       => 'sayfalar',
    'pages.create'      => 'sayfa/yeni',
    'pages.show'        => 'sayfa/{page}',
    'pages.destroy'     => 'sayfa/{page}',
    'pages.update'      => 'sayfa/{page}',
    'pages.edit'        => 'sayfa/{page}/duzenle',
];

I am trying to replace the {page} value in these paths with the slug value of the model using LaravelLocalization::transRoute(). But I couldn't. I couldn't see it on the Github page either. How can I import the model to this URL from within blade?

From: http://localhost/tr/sayfa/{page}

To: http://localhost/tr/sayfa/this-is-article-slug

Edit:

I came to the solution by defining an helper function.

if (!function_exists('trans_url')) {
    function trans_url($routename, $attributes = [], $forceDefaultLocation = false)
    {
        return LaravelLocalization::getURLFromRouteNameTranslated(LaravelLocalization::getCurrentLocale(), $routename, $attributes, $forceDefaultLocation);
    }
}

But now I realized that I don't know how it would be better to pass the $page value each time when I want this link to be in the always visible part of my page, such as sidebar.blade.php.

Example:

// $page should come in all requests and $page is available in 3 languages.
trans_url('admin.pages.show', ['page' => $page]); 
// Result should be: http://localhost/en/admin/pages/slug-name

I would be grateful if you could help.

question from:https://stackoverflow.com/questions/65648096/creating-a-laravellocalization-connection-with-a-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...