Using phpseclib in model laravel 5.0 - laravel

I'm trying to include Crypt/RSA.php in my model, but this always appear "AppClass 'App\Crypt_RSA' not found". Any idea? Thanks in advance

That error appears because you're trying to instantiate a the Crypt_RSA class in the App namespace where it doesn't exist, because the Crypt_RSA class is defined in the global namespace. So you can do one of two things:
1. Write a use statement at the top of your file:
use Crypt_RSA;
2. Prepend a backslash to the class name when using it:
new \Crypt_RSA();
You can read more on how namespaces work in the PHP Namespaces Documentation.

Related

Resolve this error for Laravel migration file: Each class must be in a namespace of at least one level

How to resolve this phpcs error? It shows up for the class Keyword in all Laravel migrations files. The error message is:
Each class must be in a namespace of at least one level (a top-level
vendor name) phpcs
You can do it several different ways:
First, you can put this at the top of your file:
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
Second, you can use:
// #codingStandardsIgnoreLine
Just before your class declaration.
Hope any one of these will work for you.
If the problem still persists please let me know about this.

Why do we use \ instead of the namespace?

I am new to Laravel and I have seen that when we use Query Builder instead of Eloquent as is required we must add the namespace i.e. use Illuminate\Support\Facades\DB; or use DB;(As it is mentioned in Aliases) inorder to use it, some people simply use \DB while writing a query.
Eg. \DB->table('tasks')->where('id', 3)->get();
What does \ denote in namespacing convention or what does it mean here. While finding answers someone told me, \ means Global Namespace, but if that's true what does it actually mean in this context? I used \ in place of App\Model_Name and I got an error. Why s that so if \ means Global Namespace?
PHP has a trick for referring to classes that are located within the global namespace, we simply prefix them with a backward (\) slash.
With the leading backward (\) slash, PHP knows that we are referring to the Class in the global namespace, and instantiates that one.
You will have an error while using (\) when some classes have same Class names.
Example:
Request class has many namespaces Illuminate\Support\Facades\Request and Illuminate\Http\Request and when you are trying to use Request class for
Illuminate\Http\Request class and you have defined \Request instead of using a namespace, the conflict will happen and you will get an error with no class found.
This is my belief. Hope this stuff will help you little to understand.

MVC CORE setting a session

I've been following the Microsoft documentation trying to set a session using this line
HttpContext.Session.SetString(SessionKeyName, "Rick");
from the page
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?tabs=aspnetcore2x
but it gives the error
'ISession' does not contain a definition for 'SetString'
There's a Set option, but that takes a byte.
Any idea what I'm doing wrong and why I'm using the wrong HttpContext.Session?
SetString,GetString,GetInt32 and SetInt32 are extension methods on ISession defined inside the Microsoft.AspNetCore.Http namespace. So to use these, you should add a using statement to this namespace in your class.
using Microsoft.AspNetCore.Http;
Now in your class, you can use these extension methods
HttpContext.Session.SetString("Test", "Rick");
HttpContext.Session.SetInt32("Age", 25);

How do you reference a model in a laravel controller? [duplicate]

This question already has answers here:
Troubleshooting referencing a model in a laravel controller
(2 answers)
Closed 6 years ago.
I'm finding the distinction between "Controller" and "Model" in laravel 5.2 to be very blurry.
I use artisan to create a RESTful controller, and in the store method, I try to create a new object.
// store in the database
$car = new App\Models\CarModel;
Then I get the error as follows:
Class 'carfreak\Http\Controllers\App\Models\CarModel' not found
So it all seems to come down to the namespace of the controller, but I don't understand it.
The name space describes the controller, right?
So why is my reference the model, being built on the controllers path? It shouldn't have anything to do with it... right?
EDIT: After trying various suggestions, I've concluded there are three things to look at:
Each class has a namespace set, correctly describing the folder where the class is located
In the controller, have the statement "Use app\models\CarModel"
refer to the model in the controller.
Each seems to be correct, but I still get the error that it cannot find the model
This is a namespace problem in php.
You just use like this.
$car = new \App\Models\CarModel;
or
use App\Models\CarModel;
....
class {
$car = new CarModel;
}
First of all check name space in Model file , Define name space in model file
namespace App\Http\Models;
And then use
use App\Http\Models\CarModel;
Well, here it is. I solved it by asking myself this question: If I'm having so much trouble namespacing, referencing and organising my models, then maybe I can get artisan to do it for me.
The post that got me thinking differently was Mansoor Akhtar's advice here: How do I instruct artisan to save model to specific directory?
Get artisan to make the model in the right place first time.
php artisan make:model Models/CarModel
In the Controller, reference the model correctly
use name-of-app\Models\CarModel;
There may or may not be cache flushing involved in my problem. I was eventially restarting my XAMPP after every change to ensure no caching was involved. I also tried
php artisan cache:clear
You'll need to add a use statement to the top of your class.
Try:
use carfreak\Models\CarModel;
class ...
This assumes that your model is in the carfreak\Models namespace, and in a Models folder within your App / carfreak folder, otherwise you'll just need use carfreak\CarModel;.
I believe you have just ran the artisan command to create the model and you didn't move the CarModel file to Models folder. (Correct me if I'm wrong)
So in your controller add this before class declaration:
use carfreak\CarModel;
Then anywhere in your controller you can access the model like this:
$car = new CarModel;

Laravel: conflict between model name and built-in facade

I have a Model in my Laravel app called Event. As I just discovered, this creates a conflict between my model and Illuminate\Support\Facades\Event, a built-in facade. The obvious solution here is to either change the name of my Model, which is not ideal because there is really no other name I could give my Model that makes any sense, or to rename the alias in app.php for Illuminate\Support\Facades\Event, which I'd like to avoid for fear of breaking anything that may rely on that alias in the future (I'm afraid I may forget).
It's been suggested that perhaps I could use namespaces, which I attempted as follows:
app/models/Event.php
namespace Models; #<-- I've also tried using "\Models" here
class Event extends \Eloquent{
app/database/seeds/DatabaseSeeder.php
Models\Event::create(); #<-- again, I've also used "\Models\Event"
All 4 combinations above have yielded a Class 'Models\Event' not found error when I run php artisan db:seed.
Perhaps I simply don't understand namespaces properly, but the more pressing issue is how to solve my problem. If it can be solved using namespaces as suggested, great, but I'm open to any other ideas as well.
I made this mistake early on as well, not necessarily understanding the role of namespace throughout the entire app.
The namespace should mark the business logic within the domain or responsibility of the app itself, so giving a namespace of Models isn't necessarily useful. Instead create a root namespace named after the app, your company, you, or whatever you like, then provide a Model sub-namespace.
For example:
namespace MyGreatApp\Models;
class Event extends \Eloquent{ }
Then you would reference this model under:
use MyGreatApp\Models\Event;
$event = new Event();
In the long run this is a cleaner and more organized approach. This does mean moving your models into a different folder, though. But there's nothing wrong with that. At least that way you know you have all your custom code in your MyGreatApp namespace. :)

Resources