emen
emen

Reputation: 6328

Laravel - Using bundle to retrieve data from db

First of all, I'm new to Laravel and MVC environment. For this project, I'm using MySQL as database and Charisma as the bundle. I'm having difficulties when trying to retrieve data from db. I wonder if someone would guide me through all these problems.

  1. If I'm using bundle, where do I put all the models, controllers and the routes? In /application folder, or in the bundle's folder itself?
  2. When do I use /application folder, and when do I use Charisma folder? (obviously, I'm only using it for the front-end UI)
  3. I have a record in my database table (named user_links) to test the data retrieval in Charisma View. How do I go about that?

I'm using this CRUD Tutorial in Laravel as a guide, but it's a little confusing when it comes to using bundle.

Ok, let me try do it my way. I'm going to do a Retrieval here. I hope someone can point out the mistake and guide me for a fix.:

Table name user_links:

id: int
user_id: varchar
link_title: text
link_url: text

Model /application/models/user_links.php (since Charisma doesn't have /models folder, I'm gonna use the default one):

class User_Link extends Eloquent{
}

Controller bundles/charisma/controllers/user_link.php (using Charisma controllers folder)

class Charisma_User_Link_Controller extends Base_Controller{

  public $restful = true;

  public function get_index(){
 $user_links =  User_Link(); 
return View::make('user_links.index')
->with('link_title', 'Link Title')
->with('link_url', 'URL');
  }
}

View /bundles/charisma/views/pages/index.blade.php:

<div >
@foreach($user_link->results as $user_links)
    {{ $user_links::link_title }}
    {{ $user_links::link_url }}
@endforeach
</div>

Routes /bundles/charisma/routes.php:

Route::controller('charisma::user_link');

That's all. Of course there will be some errors, but if you need something more please let me know. Thanks in advance.

Upvotes: 1

Views: 1150

Answers (1)

The Alpha
The Alpha

Reputation: 146239

You should use application folder for your front end UI because charisma is an admin bundle and it's been designed for applications, those have a back end support to setup and maintain various system configurations, like a custom cms.

In Laravel-3 the application folder is the default place to keep your controllers, models and views in corresponding folders and also use the application/routes.php for routing your controllers.

But, if you want to use a different bundle as charisma then you should keep the bundle in the root of the bundle directory and add the bundle name to the array in bundles.php file. For example, I've used charisma as a bundle for my admin panel in one of my projects and the name of the folder was admin where I placed charisma's files (I renamed charisma to admin)

return array(
    'admin' => array('handles' => 'hit_admin'), <-- added for admin bundle
    // more bundles..........
);

And my directory structure was domain_root/bundles/admin and admin was charisma (renamed) and also you have to use your bundles routes.php for controllers of that bundle. for example, in domain_root/bundles/admin I had a routes.php file and I've registered all routes for the admin bundle in that file and the admin bundle had it's own controllers, views, models and public folders. So, basically, I had to use those folders. All admin controllers were in domain_root/bundles/admin/controllers folder and same for the models and views too. Every bundle also has it's own public folder to keep it's own assets.

And in the admin/routes.php a route was something like this

Route::any('(:bundle)', array('as' => 'admin_home', 'uses' => 'admin::home@index'));

Also every controller in admin bundle can extend it's own base controller, for example, I used,

class Admin_Home_Controller extends Admin_Base_Controller {
    // ...
}

And there was a base.php file in admin/controllers folder which was the admin base controller and it was something like this

class Admin_Base_Controller extends Controller {

    public function __construct()
    {
        parent::__construct();

        // Add assets for admin
        Asset::add('jQuery', 'js/jquery-1.7.2.min.js');
    }

    public function __call($method, $parameters)
    {
        return Response::error('404');
    }

}

For more information you can take a look at this tutorial and read the documentation too.

Upvotes: 2

Related Questions