I am totally new to angularjs and i am practicing.
I am a bit stuck with the following, i have a profile page what loads the users details, and i do not really know how to show it, problem is im stuck with the logic.
I created 2 routes for it
Route::get('/(:any)', array('as' => 'profile', 'uses' => 'user@profile'));
Route::get('/details/(:any)', array('as' => 'profile', 'uses' => 'user@details'));
So the url actually looks like this: http://mysite.com/username
So my logic works like this, this route
Route::get('/(:any)', array('as' => 'profile', 'uses' => 'user@profile'));
Returns the profile view
this route would fetch the users data in json
Route::get('/details/(:any)', array('as' => 'profile', 'uses' => 'user@details'));
and what i am stuck with is this
I load the view, http://mysite.com/username
i get the profile page,
Laravel view load
public function get_profile($username = '')
{
return View::make('user.profile');
}
Loading the json
public function get_details($username = '')
{
$users = User::where_username($username)->get();
return Response::eloquent($users);
}
angularjs controller
function profileCtrl($scope, $http) {
//here get the url first segment somehow
// and pass it to the get
// example
// urlSegment = username
$http('details/' + username ).success(function(data){
$scope.users = data;
console.debug(data);
});
}
So my question is, am i doing this okay, or im completly not on the right track. Could someone show me an exampe for this?
Thank you
EDIT
To be more specific, i am trying to create a restful api with angularjs and laravel but im a bit stuck with it. What i do not understand is the routing logic.
example.
mysite.com/username
, What type of route logic i need to build up to fetch the data?
Because i explained above, i created 2 routes for it, and i think thats not good.
i am trying to create a restful api with angularjs and laravel but im a bit stuck with it. What i do not understand is the routing logic.
Laravel provides a number of convient ways to create a restful API. It should be noted Laravel is "the restful api" and AngularJS is the client that will use the API to show the results (i.e. be the user front end).
Laravel 4 refers to 'Resource Controllers' to help you build a restful API. Using this picture, you can see how the controller should be laid out, and what each command is specifically required to do:
Just replace "photos" with "user" and that is how the controller will be laid out. If you are using Laravel 4 you can generate a resourceful controller using Artisan:
php artisan controller:make UserController
and it will add each of the functions above ready to go.
Laravel 3 refers to 'Restful Controllers' - but the principle is the same.
If you are interested - Nettuts has a great free tutorial on creating a Restful API with Laravel 4 from scratch. If you follow this tutorial - you will have a full restful API for your user class. It goes into much more detail than is possible in this answer.
For Laravel 4 your routes will be just one line of code:
Route::resource('user', UserController);
Then you will run the follow artisan commands
php artisan controller:make UserController
php composer.phar dump-autoload
Then your User Controller will be ready to go, with all the functions scaffold, and all you need to do is put some base logic in.
edit: I just found another tutorial that explains Laravel 4 resource controllers a bit further.
Angular is a client-side framework running only in the browser. So you simply cannot create a restful API with it. A restful API always runs on the server only, so Laravel should work for that.
The way Angular is meant to be used is that the client requests the content as JSON from a restful API running on the server (with the $http module) and then the HTML is built on the client-side. See what-is-the-point-of-angularjs-routes? I also highly recommend the official tutorial.