In laravel, how to use sentinel in a controller? - php

I am using sentinel to manage my roles an permissions but in this project the permissions can be edited by the admin. so I am using a controller to assign the new permissions. however I cant use the Sentinel class in the controller archive I get the error:
Class 'App\Http\Controllers\Sentinel' not found
because of the namespace
my question is do I need to require with require_once all the Cartalyst files or is there another way?

You can use this namespace to solve your problem
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;

Related

Changing the path of models into a models directory

What I've done
I used the command php artisan make:model User without mentioning model folder as the command should have been php artisan make:model Models\User.
I am an amateur in terms of laravel and just started php and laravel last month and made a whole multi vendor project around it without the models being in models folder.
What I want
Now, I'm just trying to structure my code in a cleaner manner and facing errors as have to change the path of models wherever it is mentioned. I tried doing them but still can't get rid of the errors.
Latest version of Laravel, by default, file is already created inside the Models folder. If you have a model outside folder and what you want is to incorporate it there, the best thing to do is to change the built-in namespace at the top of everything. For example:
namespace App\Models;
If you want is to completely change the path, first create the model and then move folder manually. Finally, in namespace mention it:
namespace FolderA\FolderB\FolderC;.
Although it is always better to follow the default Laravel structure, is cleaner.
You need to modify your
Project/config/auth.php
file on
'model'=> AppName\Models\User::class,
The best thing to do is to move 1 model at a time and replace all of its import namespaces. Test your application if everything works and then continue with the next model and so on.
Example:
Move User.php from App\ to App\Models folder
Update the namespace of User.php from namespace App; to namespace App\Models;
Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
Check your application and run tests if you have them.
If everything is ok, you can continue moving the next model to the Model/ folder and repeat step 1 to 5
For the model User.php: As #saeed mentioned, update your config/auth.php if needed.
Note that IDE's these days can do the most work for you, like PhpStorm can do all the find & replaces for you when you pressing F6 on the User.php file in the tree and update its path.

I am not able to `use` or `namespace` in laravel helper file

I have created helper file in laravel. I want to use laravel functionalities.
I am not able to use use or namespace template in helper file.
code of helper file.
namespace App\Helpers;
class CommonFunctions {
public static function get_cat() {
}
}
I have linked this helper in config/app.php
When I use namespace in helper file then this error is displaying
Blade file code
Finally I found the issue.
Actually I am migrating site from laravel 5.2 to 5.7 and I was copying the old helper file to new that's why I got this error..
Previously I was using use and namespace together but after adding use statements only I solved the issue.
It might be the issue of namespace is not allowing in helper file, but in every solution and method of creating helper files there is namespace, so, I had never try to remove that line.
But now I got the solution...
Thanks for all who helped me.
Shishil Patel
I guess you have syntaxis error in your helper.
anyways try to execute php artisan config:clear and then fix all errors.
I have done everything the same way and I haven't found any problem.

Laravel File equivalent in Lumen

I can access the list of files in a directory in Laravel using this:
use File;
....
$files = File::files($path);
However, I get this error in lumen:
Class 'File' not found
Any idea how I can access list of files in a folder in lumen.
File is only avaiable in Laravel by default. Although you can still used it on Lumen Framework by doing the following.
Enable the facade in boostrap/app.php by uncommenting the following code.
$app->withFacades();
After that, you will be able to access the File class in any of your controller by adding it as:
use Illuminate\Http\File;
or you can use the Facade Class.
use Illuminate\Support\Facades\File;
Please do a composer dump-autoload to update autoload.

According to this tutorial where have I to create this class in my Laravel project (a class implementing the UserProviderInterface interface)

I am abslolutly new in PHP and moreover in Laravel framework (I came from Java).
I am following this tutorial to create a custom authentication driver:
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver
I have a very newbye doubt: at the beginning of this tutorial it show that I have to create a class that implements UserProviderInterface.
It only show the code but not where this class have to be put into my Laravel project. The only clue about its positioning is the namespace:
namespace MyApp\Extensions;
But exactly where have I to put it?
I have the following structure:
It say to put it into MyApp\Extensions but I have not MyApp and Extension folder in my project? (or maybe the nampespaces name doesn't reflect a directory structure into the project tree?)
So where have I to create this class?
if you want it to go inside MyApp\Extensions, consider MyApp as the app folder.
Then only thing to do is create a folder named Extensions inside app folder and create your UserProviderInterface.php there.
But If I were you, I'd create it under app\Auth\Providers\UserProviderInterface.php
I believe what the page author meant was to create a folder named Extensions and create the provider file under app/Extensions folder. MyApp is just a custom namespace that the author chose, that in default laravel app, it should be App.
Which means, if you create a folder Extensions under app folder, the DummyAuthProvider should then be in the namespace of namespace App\Extensions;

Laravel 5 - Moved User model to App/Models causing autoload issues

Working on a inherited Laravel Spark project that contained two User models.
One is the standard Spark model inside the App directory but the other is inside App/Models. I have combined the two models and updated the auth.php to reference the User model inside the Models directory but composer dump-autoload is saying it cannot find the App/User model.
How can I tell the autoloader that the User model is not there any more but
instead in the Models directory?
Edit:
I have changed the namespace to App/Models but still receive the error:
class_parents(): Class App\User does not exist and could not be loaded
In my terminal when running dump-autload
Second Edit:
Fixed, didn't realise the namespace was referenced so much. Did a find and replace on App\User and sorted the issue.
A standard Laravel installation will work by simply changing the namespace as others have mentioned; however Laravel Spark references the User and Team models, therefore a namespace change alone will not work.
You shouldn't edit any files inside the vendor/laravel/spark-aurelius (aurelius codename will vary depending on your version) as these changes are not tracked.
With Spark, you should add the following lines into your app/Providers/SparkServiceProvider.php:
public function register()
{
Spark::useUserModel('App\Models\User');
Spark::useTeamModel('App\Models\Team');
}
You can set your own custom App\Models directory, rather than using the above example.
Finally you will need to update any references you make to your models, e.g. update controllers from use App\User to use App\Models\User.
Source: Laravel Spark 6.0 Customization
5th Jan 2020 Update: Remember to also update STRIPE_MODEL and BRAINTREE_MODEL values in your .env to your new namespace.
Laravel Spark 9.0 removes Braintree support, therefore you only need to update CASHIER_MODEL in 9.0.
21st Dec 2020 Update: Laravel 8.x now keeps all models in the app\Models directory by default. Even if you're on an older version of Laravel (e.g. 6.x) but are using Laravel Spark 11, then you do not need to do any of the above. Laravel Spark 11 will assume your models live in app\Models.
You need to change User model namespace:
namespace App\Models;
Try renaming the namespace. :)
namespace App\Models;
change namespace, and use like this
namespace App\Models;

Categories