Everything was working fine before I installed a package https://github.com/kreait/firebase-php.
Now I get 404 error on adding new Routes.
Old Routes are working fine, but new routes are not working
Web.php
//old routes
Route::get('/', function () {
return view('welcome'); //working
});
Route::get('email', 'EmailController#sendEmail'); /working
Route::get('/counter',function() {
$counter = Redis::incr('counter'); //working
return $counter;
});
//new routes
//Firebase admin SDK
Route::get("fire","FirebaseSdkController#fire");
Route::get("say-hello", function(){
return "hello";
});
FirebaseSdkController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Messaging\CloudMessage;
class FirebaseSdkController extends Controller
{
public function fire()
{
$firebase = (new Firebase\Factory())->create();
$messaging = $firebase->getMessaging();
$topic = 'News';
$message = CloudMessage::withTarget('topic', $topic)
->withNotification("Hello") // optional
// ->withData($data) // optional
;
$message = CloudMessage::fromArray([
'topic' => $topic,
'notification' => [/* Notification data as array */], // optional
'data' => [/* data array */], // optional
]);
$messaging->send($message);
}
}
This is what I have tried so far -
php artisan route:list
shows the new routes in the list
php artisan route:clear
php artisan config:clear
composer dump-autoload
None of them solved the issue.
Update
Now, if I delete any route and do php artisan route:clear, I still can access the route. I don't know what's happening please help.
Related
I can't run this test method, what I want to do is that only the administrator user can enter the panel, if the user is only a user, it must receive the status 302 but it receives 404,
If I run php artisan route:list I can see the route
Here is my test method:
/** #test */
public function usuario_no_administrador_no_puede_entrar_al_panel()
{
$role = factory(Role::class)->create(['name' => 'usuario']);
$user = factory(User::class)->create();
$user->roles()->sync($role);
$this->actingAs($user);
$this->withoutExceptionHandling();
$this->get('/panel')->assertStatus(302);
}
Here is my web.php file:
Route::group(['middleware' => 'role:usuario', 'middleware' => 'role:administrador'], function() {
Route::get('/panel', 'PanelController#index')->name('panel.index');
});
And here is the error:
Image of error when running php artisan test
You have two middleware keys in your route group definition. You’ll need to combine them, as otherwise the last one will override any previously-set ones:
Route::group(['middleware' => ['role:usuario', 'role:administrador']], function() {
Route::get('/panel', 'PanelController#index')->name('panel.index');
});
I'm starting to learn laravel 5.5 and I'm trying to create routes depending on the session
My code in web.php is:
if(session()->has("user")){
Route::any('/profile/view',"ProfileController#view");
}
if(session()->has("admin")){
Route::any('/game/new', "gameController#new");
}
but it don´t works, it show me "page not found".
How i can do that?
RouteServiceProvider are booted before the StartSession middleware, so you cannot access session in route files. Use middleware to check instead.
Route::middleware('session.has.user')->group(function () {
Route::any('/profile/view',"ProfileController#view");
});
Route::middleware('session.has.admin')->group(function () {
Route::any('/game/new', "gameController#new");
});
To create middlewares:
php artisan make:middleware SessionHasUser
php artisan make:middleware SessionHasAdmin
Update middlewares to check the session, if it does not have corresponding session, abort the request:
app/Http/Middleware/SessionHasUser.php
public function handle($request, Closure $next)
{
if(session()->has("user")) {
return $next($request);
}
return abort(404);
}
Install Middlewares, so routing can use the middlewares
app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
...
'session.has.user' => , \App\Http\Middleware\SessionHasUser::class,
'session.has.admin' => \App\Http\Middleware\SessionHasAdmin::class,
...
],
How can I list routes specifically the api routes in my view?
For example These:
...api/user
...api/Information
The artisan command for example does list them like this:
php artisan route:list
In your controller you can get the list of routes using Artisan facade. I assume all your api routes have api string in its path.:
public function showRoutes($request) {
$routes = Artisan::call('route:list', ['--path' => 'api']);
return view('your_view', compact('routes'));
}
Edit :
You can also use Route facades getRoutes method.
$routes = [];
foreach (\Route::getRoutes()->getIterator() as $route){
if (strpos($route->uri, 'api') !== false){
$routes[] = $route->uri;
}
}
return view('your_view', compact('routes'));
I have controller like this
public function store(Request $request)
{
Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}
Show me error
There are no commands defined in the "php artisan infyom" namespace.
When I run this command in CMD it work correctly
You need to remove php artisan part and put parameters into an array to make it work:
public function store(Request $request)
{
Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}
https://laravel.com/docs/5.2/artisan#calling-commands-via-code
If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:
Route::get('clear_cache', function () {
\Artisan::call('cache:clear');
dd("Cache is cleared");
});
To run this command from browser just go to your's project route and to clear_cache. Example:
http://project_route/clear_cache
Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()
Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:
public function handle()
{
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
}
Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/
Remove php artisan part and try:
Route::get('/run', function () {
Artisan::call("migrate");
});
Command Job,
Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php
This is the Job that runs with the artisan command.
class RangeDatePaymentsConsoleCommand extends Command {
protected $signature = 'batch:abc {startDate} {endDate}';
...
}
web.php,
Path : {project-path}/routes/web.php
web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.
$router->group(['prefix' => 'command'], function () use ($router) {
Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController#abc');
});
CommandController.php,
Path : {project-path}/app/Http/Controllers/CommandController.php
This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.
class CommandController extends Controller {
public function abc(string $startDate, string $endDate) {
$startDate = urldecode($startDate);
$endDate = urldecode($endDate);
$exitCode = Artisan::call('batch:abc',
[
'startDate' => $startDate,
'endDate' => $endDate
]
);
return 'Command Completed Successfully. ';
}
Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59
It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}
php -S 127.0.0.1:8080 public/index.php
Method #1: Using route
Route::get('run-it', function () {
(new \App\Console\Commands\ThisIsMyCommand())->handle();
});
Method #2: Using command line
php artisan command:this_is_my_command
So I have these routes defined in my routes.php:
Route::post('/register', 'Auth\AuthController#Register');
Route::post('/login', 'Auth\AuthController#Login');
Route::get('/verify/{$key}', 'Auth\AuthController#Verify');
The first two works fine. But for some reason the third one [ /verify/{$key} ] throws a NotFoundHttpException.
The verify route calls the Verify() function in my AuthController as shown below.
public function Verify($key)
{
$user = User::Where('verification_code', $key);
if(!$user)
{
flash()->error('Error Occurred in verification.');
return redirect('/login');
}
$user->verified = 1;
$user->verification_code = null;
$user->save;
flash()->success('Account Successfully Verified.');
return redirect('/login');
}
When calling the php artisan route:list from the terminal the verify/{key} shows up.
Any help would be much appreciated.
Change this:
Route::get('/verify/{$key}', 'Auth\AuthController#Verify');
to
Route::get('/verify/{key}', 'Auth\AuthController#Verify');
You don't have to add $ along with the variable in the route.