izniburak / php-router

PHP Router, which also has rich features like Middlewares and Controllers is simple, useful and flexible router class for PHP.
https://github.com/izniburak/php-router/wiki
MIT License
236 stars 45 forks source link

Router Group kullanarak urlye dil sistemi ekleme #76

Open ahmtcn34 opened 6 months ago

ahmtcn34 commented 6 months ago

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

mehmethcoskun commented 5 months ago

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

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

ahmtcn34 commented 5 months ago

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

izniburak commented 5 months ago

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.

mehmethcoskun commented 5 months ago

@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 commented 5 months ago

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); } } `

ahmtcn34 commented 5 months ago

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