Comments (8)
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.
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.
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.
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.
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.
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.
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.
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)
- If there are no any attached model it send warning. HOT 2
- Update composer package name? HOT 1
- Update Packagist package name? HOT 1
- error Router HOT 1
- CakePHP installation issue HOT 3
- v3.0 XML Sitemap Listing View HOT 1
- v3.0 List Static Pages HOT 4
- SitemapAppController extends App\Controller\AppController HOT 1
- How to call the sitemap.xml HOT 2
- Test do not pass your own cs standard HOT 1
- Add handpicked results HOT 4
- Possibility to add multilanguage records
- Compress sitemap to gz
- What happens with limits? HOT 1
- Missing Docu HOT 3
- Problem with XML file when PHP short tag is enabled HOT 1
- Working with plugin tables HOT 1
- Auth and template questions HOT 1
- Package Using
- Modify routes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cakephp-sitemap.