php-router icon indicating copy to clipboard operation
php-router copied to clipboard

Router Group kullanarak urlye dil sistemi ekleme

Open ahmtcn34 opened this issue 1 year ago • 6 comments

Url yapısını aşağıdaki resimde göründüğü gibi kurguladım. Normal şekilde dil kısaltması kullanarak sayfalara ulaşabiliyorum. Örnek domain.com/tr. Fakat şöyle bir şey yapmak istiyorum. domain.com/ bu şekilde girilince yani herhangi bir dil kısaltması yoksa urlde otomatik olarak domain.com/tr adresine yönlendirmek istiyorum. Bu yapmak istediğim şey mümkün mü? Kullanım olarak bulamadım.

image

ahmtcn34 avatar Mar 27 '24 21:03 ahmtcn34

Add a router before pattern definition then redirect to url you want.

$webRouter->router->get('/', function () { header("location: https://example.com/tr"); });

mehmethcoskun avatar May 16 '24 20:05 mehmethcoskun

İstediğim tam olarak bu değil. Kullanıcının seçtiği dili alıp urlyi ona göre dinamik yönlendirmek.

ahmtcn34 avatar May 16 '24 20:05 ahmtcn34

Bunun için eklenen custom parametre için default bir değer eklemek gerekiyor ama şu an direkt olarak paket üzerinde bu tarz bir özellik yok. Tavsiyem middleware ile handle edip, ilgili default endpoint'e yönlendirmek olur.

izniburak avatar May 16 '24 20:05 izniburak

@ahmtcn34 dostum,

domain.com/ bu şekilde girilince yani herhangi bir dil kısaltması yoksa urlde otomatik olarak domain.com/tr adresine yönlendirmek istiyorum

bu sorunun cevabı idi.

mehmethcoskun avatar May 16 '24 20:05 mehmethcoskun

Middleware ile aşağıdaki şekilde handle edebilirsin dostum.

`<?php

namespace App\Middlewares;

use Symfony\Component\HttpFoundation\Request;

class HandleLocale { public function handle(Request $request) { $pathInfo = substr($request->getPathInfo(), 1); $routerIndexes = array_filter(explode('/', $pathInfo)); $locale = $routerIndexes[0]; header("location: https://expamle.com/" . $locale); } } `

mehmethcoskun avatar May 16 '24 20:05 mehmethcoskun

Middleware ile aşağıdaki şekilde handle edebilirsin dostum.

`<?php

namespace App\Middlewares;

use Symfony\Component\HttpFoundation\Request;

class HandleLocale { public function handle(Request $request) { $pathInfo = substr($request->getPathInfo(), 1); $routerIndexes = array_filter(explode('/', $pathInfo)); $locale = $routerIndexes[0]; header("location: https://expamle.com/" . $locale); } } `

Yardımınız için teşekkür ederim. Aşağıdaki gibi yaptım şu an sorunsuz çalışıyor. İhtiyacı olan arkadaşlar olursa diye paylaşmak istedim.

image

ahmtcn34 avatar May 20 '24 15:05 ahmtcn34