#500 Error

84 messages · Page 1 of 1 (latest)

short breach
#

Hey, making my first legitimate laravel & react site I have the following routes setup.

Route::get('/discord-bot', function () {
    return Inertia::render('DiscordBot');
})->middleware(['auth', 'verified'])->name('discord-bot');

Route::post('/bot/start', [BotController::class, 'start']);
Route::post('/bot/stop', [BotController::class, 'stop']);
Route::post('/bot/update-settings', [BotController::class, 'updateSettings']);

and here is my controller

class BotController extends Controller
{
    public function start()
    {
        try {
            Log::info('Starting bot...');
            return response()->json(['message' => 'Bot started successfully']);
        } catch (\Exception $e) {
            Log::error('Error starting bot: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to start bot'], 500);
        }
    }

    public function stop()
    {
        try {
            Log::info('Stopping bot...');
            // Logic to stop the bot
            Log::info('Bot stopped successfully');
            return response()->json(['message' => 'Bot stopped successfully']);
        } catch (\Exception $e) {
            Log::error('Error stopping bot: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to stop bot'], 500);
        }
    }

    public function updateSettings(Request $request)
    {
        try {
            Log::info('Updating bot settings...', $request->all());
            $settings = $request->all();
            Log::info('Settings updated successfully');
            return response()->json(['message' => 'Settings updated successfully']);
        } catch (\Exception $e) {
            Log::error('Error updating settings: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to update settings'], 500);
        }
    }
}```

No matter what I do, whenever I send a post request it receives a 500.

```js
try {
            await axios.post('/bot/start')
            console.log('Bot started successfully')
        } catch (error) {
            console.error('Error starting bot:', error)
        }```

Does anyone have any ideas? I'm stumped
#

Bashing my head against the wall because this is the closest I've got to a working site for react & laravel

#

It's not even logging any errors

quiet dagger
#

check storage/laravel.log

short breach
# quiet dagger check storage/laravel.log

[2024-11-16 01:38:11] local.ERROR: Namespace declaration statement has to be the very first statement or after any declare call in the script {"exception":"[object] (Symfony\Component\ErrorHandler\Error\FatalError(code: 0): Namespace declaration statement has to be the very first statement or after any declare call in the script at C:\Users\HomeLab\Desktop\WebDevelopment\discord-bot-manager\app\Http\Controllers\BotController.php:3)
[stacktrace]
#0 {main}
"}

#

hm..

#

great

#

im confused, it looks fine

quiet dagger
#

Show your entire controller file

#

more than likely you've got a space above the <?php

short breach
#
<?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BotController extends Controller
{
    public function start()
    {
        try {
            Log::info('Starting bot...');
            return response()->json(['message' => 'Bot started successfully']);
        } catch (\Exception $e) {
            Log::error('Error starting bot: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to start bot'], 500);
        }
    }

    public function stop()
    {
        try {
            Log::info('Stopping bot...');
            Log::info('Bot stopped successfully');
            return response()->json(['message' => 'Bot stopped successfully']);
        } catch (\Exception $e) {
            Log::error('Error stopping bot: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to stop bot'], 500);
        }
    }

    public function updateSettings(Request $request)
    {
        try {
            Log::info('Updating bot settings...', $request->all());
            $settings = $request->all();
            Log::info('Settings updated successfully');
            return response()->json(['message' => 'Settings updated successfully']);
        } catch (\Exception $e) {
            Log::error('Error updating settings: ' . $e->getMessage());
            return response()->json(['error' => 'Failed to update settings'], 500);
        }
    }
}
quiet dagger
#

try remove the extra space above that namespace declaration

short breach
#

Same error

quiet dagger
#

if you send the file here then it'll probably show it better

short breach
#

wtf

#

theres a space

#

thats not visible on my die

#

ide

#

its not visible in notepad either

#

okay

#

that space is visible with any php file i send

#

yurp still getting

[2024-11-16 04:32:02] local.ERROR: Namespace declaration statement has to be the very first statement or after any declare call in the script {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Namespace declaration statement has to be the very first statement or after any declare call in the script at C:\\Users\\HomeLab\\Desktop\\WebDevelopment\\discord-bot-manager\\app\\Http\\Controllers\\BotController.php:4)
[stacktrace]
#0 {main}
"} ```

no visible issues in the code on my ide side or in notepad
short breach
quiet dagger
#

Well you've managed to move the error from line 3 to line 4

short breach
#

the hell 😭

#

oh

#

I must have added a line somewhere reorganizing

#

Its just literally wherever that namespace is I think??

quiet dagger
#

Yeah because that's where it's finding the error

quiet dagger
# short breach

if this one is from before you moving it, then you've got 2 blank lines instead of just 1 after the <?php

#

the namespace is very picky about placement

short breach
#
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class BotController extends Controller```

tried doing it like this, same result. with an error on the namespace line
#
[2024-11-16 04:37:20] local.ERROR: Namespace declaration statement has to be the very first statement or after any declare call in the script {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Namespace declaration statement has to be the very first statement or after any declare call in the script at C:\\Users\\HomeLab\\Desktop\\WebDevelopment\\discord-bot-manager\\app\\Http\\Controllers\\BotController.php:3)
[stacktrace]
#0 {main}
"} ```
#

Ive also tried removing any space after php and making sure namespace is on the first line, to no avail.

quiet dagger
#

I can only go off of the error; I doubt it's lying so there has to be something before it

short breach
#

Just in case of any stupid invisible character

quiet dagger
#

the only other thing I can think of is that there's something being ran before that file that affects the autoloader

short breach
#

._.

#

so lost

quiet dagger
#

check what other changes have been made

#

if you've setup git correctly that is

short breach
#

I haven't setup git, but for the most part this is a normal laravel & react breeze kit thingy

#

I haven't modified anything besides the controller, routes, and views / resources

quiet dagger
#

it's still possible to accidently do something in a different file

short breach
#

Just looked through my ide history, i haven't edited any odd files that wasn't intentional

#

Literally just parsing it with php -l it says it's got something wrong

quiet dagger
#

are you using CRLF or LF linefeeds?

short breach
#

LF

#

I literally copied in this simple code, parsed it. And got the same issue.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SimpleController extends Controller
{
    /**
     * Show a simple message.
     */
    public function show()
    {
        return response()->json(['message' => 'Hello, world!']);
    }
}
quiet dagger
#

there's certainly something dodgy happening

#

it's 4am and I have no idea

short breach
#

weirdest frickin thing i can parse the auto-generated phps in controllers/auth/ just fine

quiet dagger
#

well when opening your BotController file in my local it's showing crlf encoding

short breach
#

.. sooo I copied the authenticatedSessionController.php's insides using ctrl+v (WHICH PARSES JUST FINE) into the BotController.php parsed it. and it has issues

quiet dagger
#

what are you using to edit these?

short breach
#

phpstorm

quiet dagger
#

what does it show in the bottom right for BotController? because I'm pretty sure your ide is set to the wrong line endings

short breach
#

I tried setting both the file I copied it from and the botcontroller to both LF or CLRF and still the same thing. The auto generated one parses just fine

quiet dagger
#

well the auto generated one wont generate with crlf

quiet dagger
#

it needs to be LF

short breach
#

The auto one is LF

#

I copy the auto one to the botcontroller, when its set to LF same issue

quiet dagger
#

copy + paste will still result in wrong line endings if that's what your ide is set to

short breach
quiet dagger
#

that changes it for only the current file, I'd try set it in the actual settings

#
  • restart the ide to make sure they saved
short breach
short breach
#

ffs idfk anymore, i just created a completely unique test.php and it automatically got set as lf. I do the php -l and it still says namespace issue

quiet dagger
#

file > remove bom ?

#

it's certainly an encoding problem somewhere; I've never ran into this though

short breach
#

so stupid, this is the closest I've got to doing what I want to do. And I thought I screwed up or something, but it's this stupid thing 😭

#

What does this open as for you

quiet dagger
#

LF

short breach
#

Yep no clue

quiet dagger
#

some ansi something or other

short breach
#

ah.. wonderful.. if i create the file usingg file explorer. change the name to BotController.php and write it out in notepad.. And edit it in IDE as crlf..

#

works perfectly fine..