I'm new on laravel.
I have functions on my model php. I want to use them in controller and send to view.
This is my example function.
public function select()
{
$users = DB::table('garanti')->get();
}
now I need to use this on controller and view.
In codeigniter I handle it like this:
$data['kategori'] = $this->model->select_s();
$this->load->view('admin/kategori', $data);
If you do
class Post extends Eloquent {
public function select()
{
return DB::table('garanti')->get();
}
}
You can use it in your controller:
$data['kategori'] = with(new Post)->select();
return View::make('admin/kategori')->with('data', $data);
There are in fact other ways of doing this, but static functions are not really testable, so I wouldn't use them in this case.
This is a very good LIVE example about using MVC concept in Laravel. in this scenario the Controller is calling a function from the Model class then the Controller handle the view.Take a look.
http://runnable.com/UnFiFHVGrQh1AAA_/mvc-in-laravel-for-php
Related
I have multiple controllers, with multiple methods, which all return views.
class PageController extends Controller {
public function index()
{
// do lots of stuff
return view('view.name', $lotsOfStuffArray);
}
public function list()
{
//...and so on
}
I now have the need to create an API, which performs much of the same logic as the methods above, but returns a JSON output instead:
class PageApiController extends Controller {
public function index()
{
// do lots of the same stuff
return $lotsOfStuffCollection;
}
public function list()
{
//...and so on
}
What is the best way to accomplish this without having to copy and paste code from one controller to the other?
I've tried placing a lot of the logic into traits and using them in my Eloquent models, but that still requires that I copy and paste code from controller to controller. I should also note its not viable to check expectsJson() and return a response accordingly as I have many, many methods.
Is it a good idea to have the logic stored in a parent class and then create a child controller that responds with a view and a child controller that responds with JSON?
You could abstract the logic to a service class. I have answered a similar question.
You have PageController, PageAPIController and PageService.
class PageService {
public function doStuff()
{
return $stuff;
}
}
class PageController extends Controller {
public function index()
{
$service = new PageService();
$stuff = $service->doStuff();
return $stuff;
}
}
class PageAPIController extends Controller {
public function index()
{
$service = new PageService();
$stuff = $service->doStuff();
return $stuff->toJSON();
}
protected function toJSON(){
//You could also abstract that to a service or a trait.
}
}
I can access a function in every view like this.
In my AppServiceProvide Code
public function boot()
{
$post = Post::latest()->first();
View::share(compact('post'));
}
How can i access it in every controller?
What is the best way to call a function in every controller,as i need to make sure latest record from database.
**You can write in model also using model object**
public function YourFunction(){
return "data";
}
**call it in your controller like this make model object suppose your
model name is YourModel in controller you can call like this**
$model = new YourModel();
$data = $model->YourFunction(); //calling method in controller
I'm new to Traits, but I have a lot of code that is repeating in my functions, and I want to use Traits to make the code less messy. I have made a Traits directory in my Http directory with a Trait called BrandsTrait.php. And all it does is call on all Brands. But when I try to call BrandsTrait in my Products Controller, like this:
use App\Http\Traits\BrandsTrait;
class ProductsController extends Controller {
use BrandsTrait;
public function addProduct() {
//$brands = Brand::all();
$brands = $this->BrandsTrait();
return view('admin.product.add', compact('brands'));
}
}
it gives me an error saying Method [BrandsTrait] does not exist. Am I suppose to initialize something, or call it differently?
Here is my BrandsTrait.php
<?php
namespace App\Http\Traits;
use App\Brand;
trait BrandsTrait {
public function brandsAll() {
// Get all the brands from the Brands Table.
Brand::all();
}
}
Think of traits like defining a section of your class in a different place which can be shared by many classes. By placing use BrandsTrait in your class it has that section.
What you want to write is
$brands = $this->brandsAll();
That is the name of the method in your trait.
Also - don't forget to add a return to your brandsAll method!
use App\Http\Traits\BrandsTrait;
class ProductsController extends Controller {
use BrandsTrait;
public function addProduct() {
//$brands = Brand::all();
$brands = $this->brandsAll();
return view('admin.product.add', compact('brands'));
}
}
I am trying to run a sample example using Laravel 4 and i am getting errors like, class not found. Can someone help me out here?
controller
file name : authors.php
<?php
class Authors extends BaseController {
public $restful = true;
public function get_index() {
return View::make('authors.index');
}
}
?>
routes.php
Route::get('authors', array('uses'=>'authors#index'));
Views/authors/index.php
<h1> First program in Laravel 4 </h1>
Fitst of all your authors!=Authors, so make sure of your conyroller name in the Route.
And if you want RESTful controller then you can define your route like Route::controller('baseURI','ControllerName'),
Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method..
To know more check restful-controllers
In your example you have to rename your get_index method to getIndex as L4 is camelCase
//AuthorsController.php
class AuthorsController extends BaseController {
public $restful = true;
public function getIndex() {
return View::make('authors.index');
}
}
//route.php
Route::controller('authors', 'AuthorsController');
I have a custom MVC PHP framework that has a router class, which calls a controller, which uses a model, then the controller presents the view, etc etc.
My problem is that I can't figure out technically how to allow variables to pass between the controller and the view, semantically. I could do a quick-and-dirty fix, but what I want to have is this for a controller:
class IndexController extends Controller{
var $name = "John"; // instance variable
}
And have this for a view:
<p> <?=$name?> </p>
My question is this:
How can I create a Controller->render() function, or something similar, that allows the view to access instance variables from the controller? and,
How can I do this without doing klutzy things like $data['view']['name'] = "John"; or having to write ten lines of code by default for any new controller I make. I want to do this so it's as DRY as possible.
Thanks.
Edit: FabioCosta's solution
I'm not sure I understand, so far I have my base controller like this:
<?php
class Controller{
public function __get($key){
if(isset($this->$$key)) return $this->$$key;
}
}
?>
My base view class looks like this:
<?php
class View{
public $controller;
public function render(){
$this->controller = $this;
}
?>
And I initialize from the router like this:
<?php
$controller = new IndexController();
$view = new IndexView();
$view->render();
?>
However, this doesn't work, and I know I'm doing something wrong.
Why not pass the controller that instantiates the view and use the __get magic method?
like so:
public function __get($key){
if(isset($this->$key)) return $this->$key;
}
Here is a working example View.php:
class View{
protected $_controller;
public function __construct(Controller $controller){
$this->_controller=$controller;
}
public function render(){
echo '<h1>Hello '.$this->_controller->name.'</h1>';
}
}
Controller.php
class Controller{
protected $name='fabio';
protected $_myView;
public function __get($key){
if(isset($this->$key)) return $this->$key;
}
public function __construct(){
$this->_myView=new View($this);
}
public function indexAction(){
$this->_myView->render();
}
}
And the router:
$c=new Controller();
$c->indexAction();
Controller should not be responsible for rendering output. That is something view instances should do. Rendering should happen outside the controller.
View should request data from model layer. Then, based on information it received, select the right template, assign data and render this template (or in some cases - group of templates).
Also , router should not initialize neither controllers nor views. Controller should be responsible only for processing the request.