Giter VIP home page Giter VIP logo

Comments (13)

mikehaertl avatar mikehaertl commented on May 27, 2024

Why do you use $params = !empty($_GET) ? '?'.http_build_query($_GET) : '';? This will obviously create the ?slug=... part of the URL.

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

Also look at #57 where a similar issue was reported. I've since added test cases that show, that URL creation with slugs works just fine. Check the UrlCreationTest and SlugRedirectTest where this is tested.

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

Ok, I think this is a problem with your URL rules.

First in your rule '<controller:\w+>/<action:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/<action:\w+>/slug' you should not have :\w+ in the action part on the right side. Also that slug doesn't really belong there.

You should also put the more specific rules on top, because when parsing a request, the first matching rule will be used.

I've done a test here and everything works as expected. My configuration is:

[
    'language' => 'en',
    'components' => [
        'urlManager' => [
            'class' => 'codemix\localeurls\UrlManager',
            'languages' => ['en','de','fr'],
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<controller:\w+>/<action:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/<action>',
            ],
        ],
...
]

I have a simple test action in my SiteController like:

    public function actionDemo($slug)
    {
        return $this->render('index');
    }

And I've used the LanguageDropdown as shown in the README.

If I go to site/demo/some-slug the page will render fine and the dropdown will have links like

  • /de/site/demo/some-slug
  • /fr/site/demo/some-slug

from yii2-localeurls.

vc7deo avatar vc7deo commented on May 27, 2024

Thanks for your quick response.the problem is with language switcher i made as you said

i have 2 languages so i need a simple switcher .

   $route = Yii::$app->controller->route;
   $params = $_GET;
   array_unshift($params, '/'.$route);
  <?php if(Yii::$app->language == 'ar'){ ?>
  <?= Html::a('English', [Yii::$app->urlManager->createUrl($params), 'language'=>'en']); ?>
  <?php }else{?>
  <?= Html::a('Arabic', [Yii::$app->urlManager->createUrl($params), 'language'=>'ar']); } ?>

i tried part of the code taken from your language drop down its not working .its producing url like below

http://localhost/multi/backend/web/en/multi/backend/web/ar/post/slug?slug=my-long-slug-post

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

Again, I'm pretty sure, this is a problem with your URL rules. So please first try to make it work without the localeurl extension (i.e. remove the codemix\localeurls\UrlManager and according configuration). You need to be able to create a slug URL with \yii\helpers\Url::to(['your/action', 'slug' =>'some-slug']). This doesn't work with the rules you have.

I'm closing this for now, because I can't reproduce any issue. If you find out, that URL creation works correctly without our class but fails with localeurls let us know and I will have another look.

from yii2-localeurls.

vc7deo avatar vc7deo commented on May 27, 2024

Thanks...
i tried everything nothing suceed this is the site i developed using yii2 used above default urlrule

http://flywingzdestination.com/package/malaysia-3-nights-4-days

i just need a simple switcher when we are in English site show a switcher link like Arabic and viceversa.the problem is i'm generating the url its like

http://localhost/**multi/backend/web**/en/**multi/backend/web**/ar/post/slug?slug=my-long-slug-post

as explained just above my post

i dont know why its repeating the multi/backend/web
past 2 days i'm with this without any luck for this simple switcher

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

Can you create a correct slug URL without our extension?

from yii2-localeurls.

vc7deo avatar vc7deo commented on May 27, 2024

hii after lot of trying i got solution for my first problem

   $route = Yii::$app->controller->route;
    if(Yii::$app->language == 'ar'){
        $_GET['language'] = 'en';
    }else{
        $_GET['language'] = 'ar';
    }
    $params = $_GET;
    array_unshift($params, '/'.$route);

and for link

  <?php if(Yii::$app->language == 'ar'){ ?>
  <?= Html::a('English', Yii::$app->urlManager->createUrl($params)); ?>
  <?php }else{?>
  <?= Html::a('Arabic', Yii::$app->urlManager->createUrl($params)); } ?>

Now i have one more problem.I cant hide action name when using slug

i cant use url like this

http://localhost/multi/backend/web/ar/post/my-long-slug-post

when switch it'll redirect to

http://localhost/multi/backend/web/post/slug?slug=my-long-slug-post

.But when i use default Url manager it works fine using below rules

'<controller:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/slug',

and you can see the live example used above rule

http://flywingzdestination.com/package/malaysia-3-nights-4-days

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

@vc7deo Hmm. Can you provide a simple test configuration that I can use to reproduce the issue? Preferably you should use a clean, simple fresh yii application and only add so much configuration there until the problem appears. Then post what you've changed in the default configuration here.

from yii2-localeurls.

vc7deo avatar vc7deo commented on May 27, 2024

@mikehaertl .. i use fresh yii2 application for this.Here is my Urlmanager configuration.When i comment first 3 lines it works perfectly.

        'urlManager' => [
           'class' => 'codemix\localeurls\UrlManager',
           'languages' => ['en', 'ar'],
            'enableDefaultLanguageUrlCode' => false,
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<alias:index|login>' => 'site/<alias>', 
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
                '<controller:\w+>/<action:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/<action>',
                '<controller:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/slug',
                'defaultRoute' => '/site/index',
            ],
        ],

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

This is still a problem with your URL rules. The following 2 rules match the same URL format:

'<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
'<controller:\w+>/<slug:[A-Za-z0-9 -_.]+>' => '<controller>/slug',

When you create a URL, Yii will go through all URL rules and use the first matching rule. So for example:

$url = Url::to(['somecontroller/slug', 'slug' => 'blabla'])

The first rule that matches the route is <controller:\w+>/<action:\w+>. Yii will stop there and use this rule. You need to make sure, that your URL rules are unambiguous and that more special rules are listed first.

All this has nothing to do with this extension. I'd suggest that you carefully read again about URL creation and parsing in the Yii guide here: http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

from yii2-localeurls.

vc7deo avatar vc7deo commented on May 27, 2024

@mikehaertl .. as i said before its working with default urlmanager

i used same url rules the below live site
this is controller without action(controller/slug)
http://flywingzdestination.com/package/malaysia-3-nights-4-days

and this controller with action
http://flywingzdestination.com/site/contact

both working perfectly

if i use controller/action/slug then this urlmanager works.but it would be better if i can hide the action from it

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on May 27, 2024

I'm still missing the code you used to create the URL. The URL itself doesn't help me to debug your problem.

from yii2-localeurls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.