Blank page for any new route in Laravel - laravel-5

I am using Laravel 5.0 and been developing on this project for months without any problems. I just added a new route with a new method in the controller. But whatever I do, all these new entries in the routes.php file show me a blank page.
In my routes.php:
Route::get('dashboard/product_categories/testing', 'ProductCategoriesController#testing');
In the ProductCategoriesController.php:
public function testing() {
die('Hello world!');
}
This happens to all new entries in the routes.php. No matter what controller its pointed to.
I do not use route:cache so am clueless where this problem stems from
Please note: This is an unchanged project. The httpd server is running as it should and the storage folders have read/write permissions

If you have a resource controller for the dashboard try to add the new routes before the resource.

I totally forgot about this question, but I found the solution: php artisan route:clear

For me, none of the answers helped.
My problem was with two routes matching each other.
A sample of my web.php file is below:
Route::get('user/{user}', 'UserController#show');
Route::get('user/restore, 'UserController#restore');
It took me a while to realise what was happening here.
Despite the fact that $user was typecast in my controller to an instance of User; I just assumed that the string restore would not match that route.
In the end, I realised that the string restore was being passed as a parameter to the route above and was, therefore, failing to match a User model.
To solve this I had to change the route to the following:
Route::get('user/{user}', 'UserController#show')->where('user', '\d+');
Route::get('user/restore, 'UserController#restore');
Now only numbers are matched to that route and the restore route works as expected.
The other solution would be to change the order but I didn't want to do that.

Try clearing the cache:
php artisan cache:clear

Try deleting this before public function methods :
#return \Illuminate\Http\Response

Related

Route [add.sp] not defined

i wrote url like that
use App\Http\Controllers\DashController;
Route::get('/admin/special', [DashController::class, 'addSpecializations'])->name('add.sp');
this is controller
public function addSpecializations()
{
return view('dashboard.add-specializations');
}
when i tried to open it i can't even though all route work
after that i wrote this code in view's file
<a href="{{route('add.sp')}}">
so i faced this issue
Route [add.sp] not defined.
In case your routes are cached, run php artisan route:clear. In development, don't cache anything, including views and config.

Laravel policies : code change is ignored. Is there any policy cache to clear?

I'm working full time on an application for 2 years. I encounter this bug regularily, let's say every couple months, but never manage to fix it in a reliable way because it just disappears and I never get to find the "why".
So, here it is again, and I have no clue why and how to fix it. This time, I'm writing something so I'll have a page to favorite for the next time.
Here is the bug:
If I make any change to a policy method, for instance in app/Policies/UserPolicy, the change is not taken into account when using #can in a blade or $user->can in a php file. I can introduce a die in the policy, a Log::debug('something') or even a return false at the very start of the function, but nope, still returning true.
Here is a code sample:
File : app/Policies/UserPolicy
public function deleteUser(User $user, User $target)
{
return false;
if ($user->id === $target->id) {
return false;
}
// [...]
}
Here is the code testing, it returns true, whatever I do in the policy code:
$me = Auth::user();
dd($me->can('deleteUser', $me));
Originally this example should return false, but it's returning true and I don't know why. Modifying the code does not change a thing, it's like there is a cache that nothing can clear. I've tried all the cache clearing commands I know:
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan clear-compiled
composer dump-autoload
Even restarted apache, and so on... I checked php.ini, I don't have OPCache enabled (line commented, but I tried with OPcache.enabled=0 too, no changes).
Maybe the reason is elsewhere but I don't know where to look. As I said, this bug usually disappear by itself without leaving me the time to find the cause.
Other way to reproduce the bug
In a blade, if I write:
#can('deleteUser', $user)
CAN
#endcan
It always display CAN. If I rename the function in the policy file to deleteUserr for instance, nothing changes (still returns true). However, if I change the blade code to #can('deleteUserr', $user) then I don't have the "CAN" displayed, as this function is not found and the result for unfound rule is alwways false.
Environment
WSL (Ubuntu 18.04, apache 2.4.29, php 7.2.19), Laravel 6.0.3
Thanks for any help !
EDIT / SOLVED : found the culprit !
It is a bad interaction with the composer package spatie/laravel-permission.
I have a spatie permission that is name "deleteUser" and is granted. The package has probably overloaded the "->can" method and now checks first in its permissions mechanism before going on the policy route. So my UserPolicy#deleteUser is simply ignored.
Here is the reason I found:
It is a bad interaction with the composer package spatie/laravel-permission.
I have a spatie permission that is name "deleteUser" and is granted. The package has probably overloaded the "->can" method and now checks first in its permissions mechanism before going on the policy route.
As the permission "deleteUser" is granted, the UserPolicy#deleteUser is simply ignored.

Session return null anyway

My project is using three different services and now I never can get sessions values, I've tried the laravel site tutorial and fallowing link question :
Laravel - Session returns null
But none of them worked!
In the first, i used this library:
use Session;
In a controller class :
$result = SocketHelper::sendRequest($req);
Session::put('uuid', $result->uuid);
Session::save();
Session::put('userId', $result->userID);
return Redirect::route('login_step_two');
In an other method :
$uuid = Session::get('uuid');
$userId = Session::get('userId');
But these are null! does I have to use cookie?
I recently upgrade to laravel 5.4
Please help me! It's made me confused!
Thanks.
Try saving Session explicitly like this, give it a try it worked for me, hope same for you.
Session::put('test_session', 'test message');
Session::save();
And retrieve it like this
echo Session::get('test_session');
And forget it like this:
Session::forget('test_session');
Session::save();
I understood the null result was becouse I was posting the value of $request to an other template and it was changed it the way :))) !
so easy to know !
Have you properly upgraded to laravel 5.4?
Laravel Docs
Sessions
Symfony Compatibility
Laravel's session handlers no longer implements Symfony's SessionInterface. Implementing this interface required us to implement extraneous features that were not needed by the framework. Instead, a new Illuminate\Contracts\Session\Session interface has been defined and may be used instead. The following code changes should also be applied:
All calls to the ->set() method should be changed to ->put(). Typically, Laravel applications would never call the set method since it has never been documented within the Laravel documentation. However, it is included here out of caution.
All calls to the ->getToken() method should be changed to ->token().
All calls to the $request->setSession() method should be changed to setLaravelSession().
Do you still have the rights to write session files in php session directory ?
Check the path returned by session_save_path() and check if your php user has the rights to write in it, check also if there is files in the directory and what are their creation date.

Laravel 5 Route

I did this command
php artisan make:controller BrandController
I added thefollowing line of code in the routes.php file
Route::resource('admin/brands', 'BrandController');
All the methods in the controller get fired except for the index()
I checked the routes list and it is there
URI Name Action
admin/brands admin.brands.index App\Http\Controllers\BrandController#index
I have no idea and have looked and looked and looked but cannot figure it out. Any help would be greatly appreciated
I had also added the Route
Route::resource('admin', 'ProductController');
when it should have been
Route::resource('admin/products', 'ProductController');
Took me over an hour to spot it. I am going to get some sleep

Laravel 5 maintenance mode turn on without artisan

Is there any possibility to turn on and turn off Laravel 5 maintenance without php artisan up and down commands when my website is being hosted ?
What I've done:
Route::get('site/shutdown', function(){
return Artisan::call('down');
});
Route::get('site/live', function(){
return Artisan::call('up');
});
The first route is working fine. But when I call site/live the site still is shuted down. What can cause this problem ?
If your project is already down, you cannot call another function.
What happens after you run php artisan down is that it creates a file named down inside storage/framework. After running php artisan up the file is removed.
You can create the file manually inside storage/framework. It will down your project. When you want to take your project live again, just remove the file.
I think the right answer is missing here..
You could add your route to app/http/middleware/CheckForMaintenanceMode.php
protected $except = [
//here
];
So It never would be off.
when you run artisan down. site is not available so when try to call up, your IP can't access site.
you must call down with your IP exception.
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
or add ::1 to local.
to make that in route without command
try to save this command in specific one and call it.
Laravel 8 introduced secret in maintenance mode, in which you can bypass the maintenance mode by providing a secret, then your Artisan::call would work.
You could add your routes to the $except var in CheckForMaintenanceMode middleware to bypass the check. Then your site/live route would work just fine.
In order to make your site live again using an url, you can create a live.php file which you put in laravel's public folder and then visit http://your.domain/live.php .
In the live.php file you need something like this: (check your projects directory structure if you don't use the default public folder!)
<?php
unlink(dirname(__FILE__) . "/../storage/framework/down");
header("Location: your.domain");
die;
just put
Artisan::call('up');
without route function.

Resources