Am new to laravel firebase realtime database and am trying to connect to the firebase database but because of one reason or another am not able to. I have a database in firebase console and i have included a json private key in my FirebaseController. this is the error i get.
Kreait\Firebase\Exception\InvalidArgumentException
Invalid service account specification
FirebaseController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FirebaseController extends Controller
{
public function index(){
$database = app('firebase.database');
$reference = $database->getReference('subjects');
$value = $reference->getValue();
return $value;
}
}
Web.php file
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/firebase','FirebaseController#index');
``
The problem for me was: I had set the FIREBASE_DATABASE_URL parameter without need.
So to anyone else seeing this, if your path is correct, check if you have this parameter set on your .env, if it is, remove it and run: php artisan config:cache and php artisan cache:clear.
Remember: on linux place firebase config files etc in a folder that user 'apache' can read!
So, for example, do not place such files in /home/myname/firebase.json. Even if you go chmod 777 firebase.json, this file may not be accessible by user 'apache'....
Then you do not need to use env variables.
$factory = (new Factory())->withServiceAccount(DIR.'/vendor/firebase-adminsdk.json');
Related
I know there is a heck ton of links out there about Laravel not finding some class and stuff, but I really tried everything here and nothing works. I don´t even know which code I should share here to help, so...I'll share my Newsletter controller and the web.php code
I'm using laravel 9.9 and php 8.1
web.php
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsletterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('newsletter','NewsletterController#create');
Route::post('newsletter','NewsletterController#store');
Newsletter controler
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Newsletter;
class NewsLetterController extends Controller
{
public function create()
{
return view('newsletter');
}
public function store(Request $request)
{
if ( ! Newsletter::isSubscribed($request->email) )
{
Newsletter::subscribePending($request->email);
return redirect('newsletter')->with('success', 'Thanks For Subscribe');
}
return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');
}
}
The issue is that your class name has a capital L for Letter, while on the route it does not, so your web.php should look like this:
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsLetterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('newsletter','NewsLetterController#create');
Route::post('newsletter','NewsLetterController#store');
Maybe I am not sure
I think you can't use this method to write route in laravel 9 ...because I am using larvel 8 and it is not working so better to use:
Route::get('/usernewsletter, [NewsLetterController::class, 'create']);
https://laravel.com/docs/9.x/routing
i'm trying to make an example project to learn laravel. I set up the project on my computer and everything worked fine: I have my web.php that routes / to a view, but before that it collects devices so it can show you some data.
I decided to try and host it on a Debian AWS instance so I cloned my repo in /var/www/, migrated the database, wrote the .env, composer install, set folder ownership to admin user and permissions to storage and bootstrap.
Once i configured nginx i tried it by navigating to the url and the answer was Class "App\Models\Device" not found.
I checked the namespaces, file names, I even read that debian is a sucker for caps sensitivity so i double checked every capital letter in every name and since app folder is named app and not App i even tried to import it as app\Models\Device but to no avail.
I also tried with composer dump-autoload as many on SO suggested, but nothing changed at all.
Am i missing something?
Device.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Device extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'serial',
'livello_acqua',
'umidita',
'temperatura',
'livello_mangime',
'stato_rele_1',
'stato_rele_2',
'stato_rele_3',
'stato_rele_4',
'descrizione',
];
public static function findBySerial($serial){
return Device::where('serial', $serial)->get();
}
public static function findByUser($id){
return Device::where('user_id', $id)->get();
}
}
web.php
<?php
use app\Models\Device; //\app\Models\Device - App\Models\Device - \App\Model\Device
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$devices = Device::all();
return view('index')->with(['devices' => $devices]);
});
Structure:
- app
- - Models
- - - Device
- - ...
- ...
- routes
- - web.php
- ...
I think i'm going nuts, i just tried again with App\Models\Device and got the error, re-changed it to app\Models\Device and got the error again and finally re-re-changed it to App\Models\Device and now it works...... have no idea why or how...
Try to import it like so use App\Models\Device; instead of use app\Models\Device;
See this link also
I have a quick question I know it wouldn't take so much time to fix but somehow I don't seem to easily find the solution.
I am building a basic api for a mobile application. I placed by api routes in api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/v1/meeting/record', 'App\Http\Controllers\Attendance#record');
The problem is anytime I send a request to this route it responds with page status 419 (Page Expired).
This is my record method in the Attendance Controller
public function record(Request $request)
{
return response()->json([
"message" => "student record created"
], 201);
}
I added the api/* to the excludes in verifycsrftoken.php but it didn't change anything.
Am I doing anything wrong?
php artisan route:cache
This has fixed it for me in the past.
Here is a discussion on all of the ways to clear cache:
https://dev.to/kenfai/laravel-artisan-cache-commands-explained-41e1
import your controller at the top of the controller class
App\Http\Controllers\Attendance;
then define your route as such
Route::post('/v1/meeting/record', [Attendance::class,'record']);
try this and let me know. thanks
I'm struggling to make my Feature Tests run with Laravel. I ran out of options. This is the error I get (with withoutExceptionHandling to show the URL):
• Tests\Feature\ClientTest > example
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
GET http://localhost/sunny-camping/welcome
at vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:416
412▕ * #return \Symfony\Component\HttpFoundation\Response
413▕ */
414▕ protected function renderException($request, Throwable $e)
415▕ {
➜ 416▕ return $this->app[ExceptionHandler::class]->render($request, $e);
417▕ }
418▕
419▕ /**
420▕ * Get the application's route middleware groups.
+1 vendor frames
2 tests/Feature/ClientTest.php:19
Illuminate\Foundation\Testing\TestCase::get()
Obviously if I click the URL everything works fine, but the test gives me 404... The page itself is default welcome page from Laravel. Now for the files:
ClientTest.php
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ClientTest extends TestCase
{
/**
* A basic feature test example.
*
* #return void
*/
public function test_example()
{
$this->withoutExceptionHandling();
$response = $this->get('/welcome');
$response->assertStatus(200);
}
}
web.php
<?php
use App\Http\Controllers\Admin\ClientController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/{year?}', [HomeController::class, 'home'])->where('year', '[0-9]+')->name('home');
Route::prefix('/admin')->group(function () {
Route::prefix('/clients')->group(function () {
Route::get('/add-client', [ClientController::class, 'addClient']);
Route::get('/edit/{id}', [ClientController::class, 'edit'])->name('admin.clients.edit');
Route::put('/add', [ClientController::class, 'add']);
Route::patch('/update/{id}', [ClientController::class, 'update']);
Route::delete('/delete/{id}', [ClientController::class, 'delete']);
Route::get('/paginated-json', [ClientController::class, 'paginatedJson']);
Route::get('/find-json/{id}', [ClientController::class, 'findJson']);
});
Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
Route::get('/clients', [AdminController::class, 'clients'])->name('admin.clients');
Route::get('/bills', [AdminController::class, 'bills']);
Route::redirect('/', 'admin/dashboard');
});
Route::get('/welcome', function () {
return view('welcome');
});
I'm running everything from Windows Subsystem Linux, using Apache and MariaDB.
So far I tried multiple things:
php artisan serve (no clue why but it helped some people, not me though)
Different URIs
Making .env.testing file with APP_URL set to the same as .env file
Adding APP_URL to phpunit.xml file <server name="APP_URL" value="http://localhost/sunny-camping"/>
Pasting full URLs as the URI
Copying URIs from php artisan routes:list
Using `route('myroutename')' instead of URI
All of this to no avail. I keep getting 404 and I have no clue how to fix this. I went through multiple queries and over 2 pages of Google and found no solution...
Any ideas are appreciated.
Turns out, tests act in a (to me) very weird way. They use a different APP_URL than everything else.
So to fix this, you either have to set you APP_URL to just http://localhost in your .env.testing file, or add <server name="APP_URL" value="http://localhost"/> to your phpunit.xml file, given similar file structure. (Unfortunately I don't understand this enough to be able to tell how this will behave if your project is in deeper folders or non-local server)
I have a problem, new routes in laravel are not working, url shows the correct route but almost as if it does not get to my routes web file just returns page not found every time.
I have tried:
using named route,
moving function to different controller,
clearing route cache,
clearing app cache,
dump-auto load,
made sure that AllowOverride is set to All,
Web.php:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/
Route::get('/courses', 'CourseController#index');
Route::get('/courses/create', 'CourseController#create');
Route::get('/courses/{course}', 'CourseController#show');
Route::get('/courses/{course}/edit', 'CourseController#edit');
Route::post('/courses', 'CourseController#store');
Route::patch('/courses/{course}', 'CourseController#update');
Route::delete('/courses/{course}', 'CourseController#destroy')->name('course-delete');
Route::get('/courses/statistics', 'CourseController#statistics');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/section/{section}', 'SectionController#show');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/progress', 'UserProgressController#index');
Route::get('/progress/create', 'UserProgressController#create');
Route::get('/progress/{section}', 'UserProgressController#show');
Route::get('/progress/formativeresults', 'UserProgressController#formativeresults');
//Route::get('/progress/coursestatistics', 'UserProgressController#coursestatistics');
//Route::get('/progress/{progress}/edit', 'UserProgressController#edit');
Route::post('/progress', 'UserProgressController#store');
//Route::patch('/progress/{progress}', 'UserProgressController#update');
//Route::delete('/progress/{progress}', 'UserProgressController#destroy')->name('progress-delete');
Controller:
public function statistics()
{
dd('Test');
return view('coursestatistics');
}
View file name:
coursestatistics.blade.php file structure views/coursestatistics
Link to page:
<a class="navbar-brand" href="/courses/statistics">
{{ __('Statistics') }}
</a>
Can anyone tell me what might be causing route not to work?
Try placing
Route::get('/courses/statistics', 'CourseController#statistics');
below this particular line of route code
Route::get('/courses/create', 'CourseController#create');
The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here
I had same issue, Did all those magic with configs and nothing..
Solution: run: php artisan route:clear
If issue remains same After cache clear or laravel routing rule, use 'composer dump-autoload'
If in your controller you have Model::findorFail($id) and an object with that ID does not exist, it can also lead to this error (in Laravel 6)