Skip to content

Instantly share code, notes, and snippets.

@jakecleary
Created April 30, 2015 16:44
Show Gist options
  • Save jakecleary/8db4cd4434deefbcae53 to your computer and use it in GitHub Desktop.
Save jakecleary/8db4cd4434deefbcae53 to your computer and use it in GitHub Desktop.
Touchline Snippets
<?php namespace Touchline\Repositories;
use Illuminate\Container\Container;
use Touchline\Repositories\RepositoryInterface;
/**
* Provides default implementations of the methods
* defined in the base repository interface.
*/
abstract class AbstractRepository implements RepositoryInterface {
protected $modelName;
protected $model;
public function __construct(Container $app)
{
$this->model = $app->make('\\Touchline\\' . $this->modelName);
}
public function create(array $attributes)
{
return $this->model->create($attributes);
}
public function all($columns = ['*'])
{
return $this->model->all($columns);
}
public function find($id, $columns = ['*'])
{
return $this->model->find($id, $columns);
}
public function destroy($ids)
{
return $this->model->destroy($ids);
}
}
<?php namespace Touchline\Repositories;
interface RepositoryInterface
{
public function create(array $attributes);
public function all($columns = ['*']);
public function find($id, $columns = ['*']);
public function destroy($ids);
}
<?php namespace Touchline\Http\Controllers\Admin;
use Touchline\Http\Requests;
use Touchline\Http\Controllers\Controller;
use Touchline\Repositories\UserRepositoryInterface as Users;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function __construct(Users $users)
{
$this->users = $users;
}
/**
* Retrieve a list of the users.
*/
public function index()
{
$users = $this->users->all();
return view('admin.users.index')->with(['users' => $users]);
}
public function show($id)
{
$user = $this->users->find($id);
// dd($user);
return view('admin.users.show')->with([
'user' => $user
]);
}
/**
* Delete a user.
*
* @param integer $id The user's id
* @return Response Redirect to the admin user index
*/
public function destroy($id)
{
$this->users->destroy($id);
return redirect()->route('admin.users.index');
}
}
<?php namespace Touchline\Repositories;
use Touchline\Repositories\AbstractRepository;
use Touchline\Repositories\UserRepositoryInterface;
/**
* Implement any methods specific to the User model here.
*/
class UserRepository extends AbstractRepository implements UserRepositoryInterface
{
/**
* The name of the model we wan't to load.
*
* @var string
*/
protected $modelName = 'User';
/**
* Find a user by their display name.
*
* @param string $displayName The user's display name
* @return \Illuminate\Database\Eloquent\Model
*/
public function findByUserName($displayName)
{
return $this->model->where($displayName)->get();
}
}
<?php namespace Touchline\Repositories;
use Touchline\Repositories\RepositoryInterface;
/**
* The UserRepositoryInterface contains ONLY method signatures for methods
* related to the User object.
*/
interface UserRepositoryInterface extends RepositoryInterface
{
public function findByUsername($username);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment