sourcetip

Artisan Command에서 WordPress 기능에 액세스하는 방법

fileupload 2023. 9. 20. 20:40
반응형

Artisan Command에서 WordPress 기능에 액세스하는 방법

저는 Laravel과 함께 WordPress를 사용하는 이 튜토리얼을 따랐고, Laravel 컨트롤러에서 WordPress 기능에 접근할 수 있었습니다.

기본 예시

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Corcel;
class WordPressController extends Controller
{
    public function getIndex ()
    {
        return redirect('/');
         $posts = get_posts([
             'posts_per_page' => 20,
             'order' => 'ASC',
             'orderby' => 'post_title',
             ]);

        return $posts;
    }

그것은 효과가 있고 지금까지 시도했던 모든 워드프레스 방법들을 접할 수 있었습니다.

이슈

새로운 장인 명령을 생성하고 등록한 후 거기서 동일한 메서드에 액세스하려고 할 때 막힙니다.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Http\Request;
use App\Http\Requests;

class WPTags extends Command
{
    protected $signature = 'wp:tags';
    protected $description = 'Output tags from WordPress';

    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {
        $tags = get_tags([
          'number'=>20,
          'offset' => 10,
          'hide_empty' => true,
        ]);
        return $tags;
    }

Laravel 5가 index.php 파일을 통해 WordPress 메서드를 가져오는 방법에 문제가 있음을 알 수 있습니다.오토로딩을 해야 할 것 같은데 길을 잃었습니다.artisan commands file constructor 안에 있는 index.php file에 있는 단계를 반복해 보았습니다.

내가 생각할 수 있는 유일한 다른 (해킹) 것은 내 장인 명령에 컨트롤러를 가져오는 것이었지만 나는 정말로 그렇게 하지 않을 것입니다.

갱신하다

인정된 답은 가야 할 길입니다.마주칠 일이 몇 가지 있습니다.다음과 관련된 몇 가지 오류가 발생할 것입니다.$_SERVER클라이언트에 변수가 설정되어 있지 않습니다.여기 제가 이러한 오류를 억제/처리하는 데 사용한 코드가 있습니다.

완벽하지는 않지만 지역 발전을 위해서는 최소한 생산성은 확보할 수 있을 것입니다.

//assumes you're using localhost as your base url
$_SERVER['HTTP_HOST'] = "localhost";
$_SERVER['SERVER_PROTOCOL'] = "HTTP/1.1";

if (!isset($_SERVER['REQUEST_METHOD'])) {
  $_SERVER['REQUEST_METHOD'] = "GET";
}
if (!isset($_POST['action'])) {
  $_POST['action'] = "undefined";
}

define('WP_USE_THEMES', false);

require __DIR__."/../public/wordpress/wp-blog-header.php";

저는 이 부분이.

라라벨을 워드프레스에 연결

define('WP_USE_THEMES', false);
require __DIR__."/../public/wordpress/wp-blog-header.php";

더 나은 위치에 배치되는 것이app.php이 파일은 웹, 콘솔 등 라라벨과 통화할 때마다 열립니다.이것은 테스트된 것은 아니지만, 저는 그것이 효과가 있을 것이라고 생각합니다.

다른 하나는 이 파일을 작곡가 오토로더에 포함시키는 것이 훨씬 더 좋은 방법이라고 생각하지만 상수를 정의할 수는 없습니다.

언급URL : https://stackoverflow.com/questions/32529425/how-to-access-wordpress-functions-from-artisan-command

반응형