donnikitos / vite-plugin-php

Vite's speed and tooling to preprocess PHP-files!
https://www.npmjs.com/package/vite-plugin-php
MIT License
40 stars 0 forks source link

PHP header('Location: ' . $url) not working #21

Closed JohnAntoine closed 4 months ago

JohnAntoine commented 4 months ago

When trying to do a 301 or 302 redirect using

 header('Location: ' . $url)

I get Warning: Cannot modify header information - headers already sent by (output started at /node_modules/vite-plugin-php/dist/router.php(27) : eval()'d code:1) in /node_modules/vite-plugin-php/dist/router.php(27) : eval()'d code on line 2

An index.php with nothing but a header('Location: http://localhost:3000/some-place'); in it produces that error.

Here's my vite.config.js in case something is wrong with it. I even commented out the server.proxy section in case it interfered with something.

import { defineConfig } from 'vite';
import { fileURLToPath } from 'node:url';
import usePHP from 'vite-plugin-php';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { ViteEjsPlugin } from 'vite-plugin-ejs';
import { imagetools } from 'vite-imagetools';

export default defineConfig(({ command }) => {
  const publicBasePath = '/';
  const base = command === 'serve' ? '/' : publicBasePath;
  const BASE = base.substring(0, base.length - 1);

  return {
    base,
    plugins: [
      imagetools(),
      usePHP({
        entry: ['index.php', 'pages/**/*.php', 'partials/**/*.php'],
      }),
      ViteEjsPlugin({
        BASE,
      }),
      viteStaticCopy({
        targets: [
          { src: 'system', dest: '' },
        ],
        silent: command === 'serve',
      }),
    ],
    define: { BASE: JSON.stringify(BASE) },
    resolve: {
      alias: {
        '~/': fileURLToPath(new URL('./src/', import.meta.url)),
      },
    },
    publicDir: 'public',
    server: {
      port: 3000,
      strictPort: true,
      // proxy: {
      //   '^/[a-zA-Z0-9](-*[a-zA-Z0-9])*$': {
      //     target: 'http://localhost:3000',
      //     changeOrigin: true,
      //     rewrite: (path) => `/pages/${path}`,
      //   },
      //   '/index': 'http://localhost:3000/index.php',
      // },
    },
    build: {
      assetsDir: 'public',
      emptyOutDir: true,
    },
  }

});

Doing npm run build and running a PHP server in the dist directory works and redirects as expected.

Edit

It seems like any operation related to the header fail the same way, even in the starter repo. Trying to session_start() in system/main.php of the starter repo produces the same error.

donnikitos commented 4 months ago

Interesting most likely that has something to do with my php-assembly wizardry. I but I am pretty sure I know where the issue is.

JohnAntoine commented 4 months ago

Feel free to let me know if I can help in any way I can. Your plugin is the most streamlined solution I found while looking for a way to run a modern PHP stack. Thank you for your work.

donnikitos commented 4 months ago

Awesome that you like the solution - obviously I hope to improve it so far, that it becomes some sort of a modern PHP go-to solution.

Regarding the error: Can you maybe show me an example, how you are using the header()? I understand why the error might be happening, but for some reason I am not able to reproduce it.