Git Product home page Git Product logo

Comments (8)

beporter avatar beporter commented on June 7, 2024

As mentioned in the README, you can use the loc Behavior config property to specify the name of the Model method you want to use to generate a URL for that Model.

If you create a custom ModelName::specialUrl($Model, $primaryKey) method to return the correct URL, then set that in the Behavior's config:

'loc' => 'specialUrl',

We haven't heavily tested this since buildUrl works fine for us so it may take a bit of experimentation to get it right.

Also, using named params is discouraged and support for them will be dropped in 3.0. You should consider switching to query string params as soon as you are able.

from cakephp-sitemap.

nick-harper avatar nick-harper commented on June 7, 2024

I am only using the routes to return them and setting the links like this:

$this->Html->link('link text'', array(
'controller' => 'offers',
'action' => 'view',
'id' => $item['Offer']['id'],
'slug' => $item['Offer']['slug'],

            ), array('escape' => FALSE));

The reverse routing is then working them out. I don't have a specialURL set.

Could I create a new buildURL function with edited values?

public function buildUrl(Model $Model, $primaryKey) {
    return Router::url(array('plugin' => NULL, 'controller' => Inflector::tableize($Model->name), 'action' => 'view', $primaryKey), TRUE);
}

from cakephp-sitemap.

beporter avatar beporter commented on June 7, 2024

Yes, specialUrl() was only an example. You should be able to override buildUrl() in your Model instead without issues, provided it takes the correct arguments and still returns the results from a call to Router::url() like you've demonstrated.

from cakephp-sitemap.

nick-harper avatar nick-harper commented on June 7, 2024

Do you have any examples on how you would over ride the buildURL? Would I need to add another entry into the config array called slug and pass it over to the build function also?

from cakephp-sitemap.

beporter avatar beporter commented on June 7, 2024

Place the buildUrl() method you demonstrated above in your Model's class.

Model/Offer.php:

class Offer extends AppModel {
  // ...
  public function buildUrl(Model $Model, $primaryKey) {
    return Router::url(array('plugin' => NULL, 'controller' => Inflector::tableize($Model->name), 'action' => 'view', $primaryKey), TRUE);
  }
}

Then modify that copy of the function to return whatever customer URL structure you need. Your Model method will get called before the Behavior's copy will.

from cakephp-sitemap.

beporter avatar beporter commented on June 7, 2024

You should only need the $primaryKey to build the URL.

Remember: You're inside the Model. If you need to lookup the correct slug, then do a $offer = $this->find('first', array('conditions' => array('Offer.id' => $primaryKey))); in your buildUrl() method and grab the slug from the results.

You can use a Cache if performance is going to be an issue, but you can refer to the Book for that.

from cakephp-sitemap.

nick-harper avatar nick-harper commented on June 7, 2024

I can't seem to get the model to over-ride, I have put the function inside the model but it seems to just use the main one still.

I even tried changing the name (and changing the name in the loc array but it is still using the default settings somehow.

from cakephp-sitemap.

nick-harper avatar nick-harper commented on June 7, 2024

Thankyou, this has sorted it:

public function OfferUrl($primaryKey) {
$offer = $this->find('first', array('conditions' => array('Offer.id' => $primaryKey)));

    return Router::url(array('plugin' => NULL,
        'controller' => 'offers',
        'action' => 'view',
        'id' => $primaryKey,
        'slug' => $offer['Offer']['slug'],
    ), TRUE);

}

As well as deleting the cache before I test.

from cakephp-sitemap.

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.