This may be simple but I just want to clarify something does the Auth class in Laravel requires a database to work? Cause I've been practicing user authentication without the database, just a simple string comparison .. thanks :)
Yes, Laravel 4 Auth requires the use of a database.
There are two Auth drivers, one is called database and the other one, which is default, is called eloquent which also makes use of your database using Laravel's very own ORM layer.
Related
For a project at work I'm creating an API in Laravel. I wanted to use MongoDB as database driver which is new to me. So I want to use Eloquent with MySQL in the beginning and when I'm confident enough switch to MongoDB.
I was reading some tutorials about the repository pattern in Laravel and saw that some returned an eloquent model, like this one. It seems to me that when returning a model on for example create($data), you're limited to database drivers which are suitable with Eloquent. I was wondering if it is a good practice to return an eloquent model. Because if I want to use MySQL for now and in the future MongoDB (without the Laravel MongoDB package), I need to rewrite some code in the controllers because the use the eloquent models instead of an array (for example). Maybe a 'wrapper' between the model and your code is a possibility? Any good recommendations, tips or thoughts on this?
If you're wondering why I want to use MySQL for now and later switch to MongoDB, the reason is there is a time limit on the project. I'm still learning Mongo. So to fulfil the needs of my employer, I'm not using MongoDB until finishing the first version / prototype.
If you use eloquent and its methods, you should be good. Just do not use the DB::raw() method as it may break your query if it does not match the DB engine you are currently in.
I wrote a repository pattern article if you want to look at it (with tdd).
But if you really want future proof, you can mix Doctrine w Laravel since Doctrine is a Data Mapper Pattern. Link
With laravel-doctrine package, you persist the data in your db engine that you prefer.
Is something like laravel-mongodb good?
Extending Eloquent to use MongoDB instead of MySQL.
...or I really need to create a class for each Table? then.. everytime i changes table structure i need to update the code..
You could use an ORM (Object-relational mapper) such as Eloquent, which is included in Laravel, and then just create an model (class) for each table in your database. Eloquent automatically maps each field into a PHP object. If you haven't ever used an ORM, I highly recommend you check out Laravel... it's what made me stick with PHP and I do almost all my projects using Laravel. Best of luck!
Adding to BakerStreet Response.
Eloquent fits your needs as the ORM itself will fetch all the columns you specify if you leave it as default. By default the drivers that it works with are: mysql, postgreSQL, and Sqlite.
Eloquent itself can be downloaded without Laravel being involved.
Please refer to Jeffrey Way's Laracast for instruction:
https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel
...thanks all, ended up writting my own magic class:
DBIntrd - Simple PHP framework for SQLite3 databases
Tired of spending time writting a bunch of code to create PHP classes & methods for SQLite tables?
DBIntrd is magic way to dinamically instance objects and persists data at SQLite3 tables..
I know that ORM and redis is weird enough but. My redis server is a storage (not temporary). So I discovered some queries and commands but now I want to know how to use it in best way. As everybody know ORM is good enough (ActiveRecord, Doctrine2, etc.). So give me your thoughts how to create model and use it with redis db.
My thoughts is to create some abstract class that will load (predis) and work with it BUT I have no idea how to make checker\manager of 'columns' (yes I know that redis has not such things as columns), simple example, we call this one
hset('user:id_111', 'username'. 'admin')
hset('user:id_111', 'password'. 'pass')
hset('user:id_111', 'email'. 'some#mail.com')
hset('user:id_111', 'confirmed'. '1')
After that we should have model that will return all method we need (they will be writted manually)
$oModel->getUserName();
$oModel->getPassword();
$oModel->getEmail();
$oModel->getConfirmed();
So now question is of calling queries, basically in abstract model we should create Predis\Client() but hey, it will be created in EACH model (is it bad?)!
Also do we have check ALL vars\values when changing data in model, simple example what should we do on incorrect data? Exception or simply not saving model and save error in model?
Any ideas\thoughts you have, please share.
PS: no need code, enough will be description how you see this one
I recently needed to tackle the same problem; I started building an orm for redis. It's still early in development, but might help you out https://github.com/tystr/redis-orm.
Laravel's auth class requires a database in order for it to be used right?
Does that consider database coming from the server having their own authentication process ?
Cause I want to use Laravel's Auth class methods where I can distinguish if the user is logged in or not so that I can prevent them(who have logged out) from clicking the back button.
Laravel 4 by default support two authentication driver, 'Eloquent' and 'Database', refer to the file you have in your app\config\auth.php, this is set to Eloquent by default.
The Eloquent ORM itself by default tied to a table in your database, though I have seen it creating tables by itself when no tables are found (maybe someone else can clarify this).
If you are not using the Auth::attempt() then it is of best practice to call the Auth::login() or Auth::loginUsingId() in your application. However this depends on how far you want to use your own authentication method (or how much you want to use the bundled authentication, for that matter).
The Title may not be as clear but Il explain what's my problem.
Im building PHP MVC framework for my project. I know there are awsome PHP frameworks, but I like to code and Im doing this to learn more about PHP and MVC and other OOP patterns.
It works great, at least components I built so far.
I use PHP 5.3 and namespaces, so I can require/load classes based on their namespaces/names.
I built SPR-0 class loader class and it enables me to use other libraries that use SPR-0 "standard"/convention like Doctrine or Symfony2 components inside my framework. And all functionality of the framework itself, i call it Core, is writen as a component. So i have \Core\Controller\Controller() class or \Core\Router\Dispatcher() class or \Filesystem\FileManager() class. So I use them where I need them. And Core components enables me to add routes, detect them, call aproppriate controller/action etc... to build an MVC basicly.
And now I need Authentication modul to check if user is loged in on protected pages.
How do I setup that? The bigest problem is how do I tell Authentication Module what tables to use? Where to find usernames and where to find passwords? How do I configure Authentication module, so it knows where to look for username and password?
I could setup users table in database and never change it, and then instruct Authentication where to find stuff he needs. But what if on next project I would like to use different database design, and i would like to use email row instead of username?
Hope you understand whats bothering me...
The short question is how to setup Authentication class/module so you can configure it later to use other rows to fetch data from, and how flexible can that class can be, as far as configuration goes? Should I map some where in configuration that variable username maps to table users row username, so i can change it latter to email? How do you build flexible and configurable Auth library?
The question is long, so thanks for reading...
If I understand you correctly, you want to be able to choose different DB tables for Auth depending upon what project you're working in...
Well why don't you create a config file that gets 'read' by the framework first thing?
That's what other frameworks do I think. You provide host,dbname,user and so on... In your case you'd, in addition to that, write in the config what tables and fields to use for authentication/auhorization?