简体   繁体   中英

Laravel9 deployment on shared hosting , New added API Routes not found Postman 404 Error

I am little new to this. I have deployed a laravel project on hostinger shared hosting.I have hosted and tested with postman it was working fine (AdminController). today i have added few more new routes(PatchController & CustomerController-Both Not Working) and just uploaded files and folder directly to their respective location below Code and server directory stucture's pictures has been also attached for your reference:

app/Http/Controllers/Patch/PatchController.php

<?php

namespace App\Http\Controllers\Patch;

use App\Providers\RouteServiceProvider;
//use Closure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Patch;
use Illuminate\Support\Facades\Hash;

class PatchController extends Controller
{
    
    public function index()
    {
        //
        return Patch::all();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $patch= Patch::where('patch_name', $request->patch_name)->first();
        // print_r($data);
            if (!$patch) {
                $request->validate([
                    'patch_name'=> 'required',
                    'emp_id'=>'required',
                    's_location'=> 'required',
                    'e_location'=> 'required',
                    'p_status'=> 'required'
                ]);
        
                return Patch::create($request->all());
                
            }
              
            return response([
                'message' => ['This patch already exist.']
            ], 404);
        

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\patch  $patch
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return Patch::where('patch_id',$id)->get();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\patch  $patch
     * @return \Illuminate\Http\Response
     */
    public function edit(patch $patch)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\patch  $patch
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $product= Patch::find($id);
        $product->update($request->all());
        $result=$product->save();
        if($result)
        {
            return ['result'=>'Data has been updated'];
        }
        else
        {
            return ['result'=>'update operation has been failed'];
        }
        return $product;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\patch  $patch
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product= Patch::find($id);
        $result=$product->delete();
        //$result=$product->save();
        if($result)
        {
            return ['result'=>'Data has been deleted'];
        }
        else
        {
            return ['result'=>'update operation has been failed'];
        }
        return $product;
    }
}

app/Models/Patch.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;



class Patch extends Model
{
    use HasFactory;
    protected $fillable=[
        'patch_name',
        'emp_id',
        's_location',
        'e_location',
        'p_status'
    ];
}

And I have replaced the old api.php with the below code.

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Patch\PatchController;
use App\Http\Controllers\Customer\CustomerController;
//use App\Models\Patch;


/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


Route::group(['middleware' => 'auth:sanctum'], function(){
    //All secure URL's
    
    Route::get('/admins',[AdminController::class,'index']);
    Route::post('/store',[AdminController::class,'store']);
    Route::put('/update/{id}',[AdminController::class,'update']);
    Route::get('/search/{id}',[AdminController::class,'show']);
    Route::delete('/delete/{id}',[AdminController::class,'delete']);
    
    // AdminController is working fine

    Route::get('/patches',[PatchController::class,'index']);
    Route::post('/c_patch',[PatchController::class,'store']);
    Route::put('/u_patch/{id}',[PatchController::class,'update']);
    Route::get('/s_patch/{id}',[PatchController::class,'show']);
    Route::delete('/d_patch/{id}',[PatchController::class,'destroy']);
    // PatchController is not working.

    //Route::get('/patches',[PatchController::class,'index']);
    Route::post('/c_customer',[CustomerController::class,'store']);
    //Route::put('/u_patch/{id}',[PatchController::class,'update']);
    //Route::get('/s_patch/{id}',[PatchController::class,'show']);
    //Route::delete('/d_patch/{id}',[PatchController::class,'destroy']);
    // CustomerController is not working.
    
});
Route::post("login",[AdminController::class,'log']);
Route::get('clear', function() {
    Artisan::call('optimize:clear');
    return redirect()->back();
     //return view('welcome');
});
/*
Route::post('loginWithOtp', 'UserController@loginWithOtp')->name('loginWithOtp');
Route::get('loginWithOtp', function () {
    return view('auth/OtpLogin');
})->name('loginWithOtp');
*/

**Note:

  1. Same code is working fine on localhost but throwing a 404 error on live server with postman.
  2. AdminController is working fine but PatchController & CustomerController giveing 404 Not Found error. Any help with highly appreciated**

Server Directory Structure: Models Path

Controllers Path / Folder Structure

Server Folder Structure

Server Folder Stucture

Have you tried delete cached files?

Delete everything in bootstrap\cache and try again on host.

You might cached routes to optimize speed and when you cache routes, you should generate new file to routes be accessible, because laravel use cached file instead of looking for api.php and web.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM