Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [CakePHP] Refaktor powielającego się kodu
kinimod99
post 22.01.2023, 14:59:49
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 22.01.2023

Ostrzeżenie: (0%)
-----


Hej, zrobiłem podział sitemapy, lecz mam problem z powtarzalnym kodem i chciałbym uniknąć tworzenia dodatkowych funkcji, żeby tylko zmienić wartość offsetu przy robieniu query (pobieram artykuły po 2000).
  1. public function index() {
  2. $this->layout = false;
  3. $sitemaps = [
  4. ['loc' => (APP . '\Config\Config')::getUrl() . '/sitemaps/articles0/sitemap.xml'],
  5. ['loc' => (APP . '\Config\Config')::getUrl() . '/sitemaps/articles1/sitemap.xml']
  6.  
  7. ];
  8.  
  9. $this->set('sitemaps', $sitemaps);
  10. }
  11.  
  12. public function articles0() {
  13. $this->loadModel('Article');
  14.  
  15. $articles = $this->Article->query(
  16. 'SELECT url, modified FROM articles WHERE site_id='
  17. . $this->siteId . " AND status='publish' AND (redirect_url is null OR redirect_url = '') AND landing_page_id is null AND calc_id = '0' LIMIT 2000"
  18. );
  19.  
  20. foreach ($articles as $article) {
  21. $urls[] = [
  22. 'loc' => (APPNAME . '\Config\Config')::getUrl() . '/-' . $article['url'],
  23. 'lastmod' => date('Y-m-d\TH:i:s+00:00', strtotime($article['modified']))
  24. ];
  25. }
  26.  
  27. $this->set('urlset', $urls);
  28. $this->render('sitemaps/urlset', false);
  29. }
  30.  
  31. public function articles1() {
  32. $this->loadModel('Article');
  33.  
  34. $articles = $this->Article->query(
  35. 'SELECT url, modified FROM articles WHERE site_id='
  36. . $this->siteId . " AND status='publish' AND (redirect_url is null OR redirect_url = '') AND landing_page_id is null AND calc_id = '0' LIMIT 2000, 2000"
  37. );
  38.  
  39. foreach ($articles as $article) {
  40. $urls[] = [
  41. 'loc' => (APPNAME . '\Config\Config')::getUrl() . '/-' . $article['url'],
  42. 'lastmod' => date('Y-m-d\TH:i:s+00:00', strtotime($article['modified']))
  43. ];
  44. }
  45.  
  46. $this->set('urlset', $urls);
  47. $this->render('sitemaps/urlset', false);
  48. }


Chciałbym mieć tylko jedną funkcje articles(), tylko pojawia się problem, że muszę podać nazwę funkcji i nie wiem jak je dynamicznie generować. Pozdrawiam
Go to the top of the page
+Quote Post
viking
post 23.01.2023, 07:47:01
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


  1. public function articles(int $offset, int $rowCount){}


--------------------
Go to the top of the page
+Quote Post
kinimod99
post 23.01.2023, 22:39:52
Post #3





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 22.01.2023

Ostrzeżenie: (0%)
-----


dziękuje bardzo, a czy mógłbym przeciażać w php metodę, żeby mieć 1 metodę z tą samą nazwą tylko parametrami by się różniła?
Go to the top of the page
+Quote Post
Salvation
post 24.01.2023, 00:09:03
Post #4





Grupa: Zarejestrowani
Postów: 344
Pomógł: 70
Dołączył: 15.07.2014

Ostrzeżenie: (0%)
-----


Nie da się przeciążać metod w PHP, ale możesz wstrzyknąć Obiekt z różną ilością properties, ale zapiętym jednym Interface'em.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 19:04