Application routes not working - laravel-4

I've just written some routes (app\routes.php) based on Laravel framework as following,
Route::model('cat', 'Cat');
Route::get('/', function()
{
return "All cats";
});
Route::get('/cats', function()
{
$cats = Cat::all();
return View::make('cats.index')->with('cats', $cats);
});
Route::get('/cats/breeds/{name}', function($name)
{
$breed = Breed::whereName($name)->with('cats')->first();
return View::make('cats.index')->with('breed', $breed)->with('cats', $breed->cats);
});
Route::get('/cats/{cat}', function(Cat $cat)
{
return View::make('cats.single')->with('cat', $cat);
});
Route::get('/cats/create', function()
{
return "Cat created.";
});
All routes are okay, except the one /cats/create.
I've tried to create other two dummies routes /dogs and /dogs/xxx, and the second one (/dogs/xxx) is not working.
It sounds weird but it actually happens. Has anyone face this problem before? Or you can provide me some hints to workout.

Maybe you need to put Route::get('/cats/create' before Route::get('/cats/{cat}. Right now system considers your create a {cat}.

Related

How to set cookie after login laravel

how to set cookie after login laravel I want to do somethin like this
in web.php
Route::middleware(['auth'])->group(function () {
if(Auth::user()){
Cookie::queue('test', '1231231', 333);
}
});
but my code didnt work
UPDATE
Route::middleware(['auth'])->group(function () {
if(\Auth::user()){
Route::resource('admin/home', 'Admin\HomeController');
Route::resource('admin/promotion', 'Admin\PromotionController');
Cookie::queue('test', '1231231', 333);
}});
is because Auth::user() not available in the route file, unless you use a middle ware to do so,
or else you can try this way
Route::middleware(['auth'])->group(function () {
if(\Auth::user()){
Cookie::queue('test', '1231231', 333);
}});

How to get the logged in user id in javascript files in laravel

I am following below link for event broadcasting in laravel.
https://laravel.com/docs/5.3/broadcasting#concept-overview
but I do not understand how to get ${orderId} in js file in below code.
Echo.private('order.${orderId}')
.listen('ShippingStatusUpdated', (e) = > {
console.log(e.update);
});
Any help?
Thanks in advance.
You would more than likely have this stored on a component. The example above would probably be more specific to an orders page or an orders component where you (I would imagine at least) already have the id so you can listen for events that are specific to that order.
So, something like:
data() {
return {
order: {}
};
},
mounted() {
Echo.private('order.${this.order.id}')
.listen('ShippingStatusUpdated', (e) => {
console.log(e);
});
}
When you define your Event you specify the channel you channel you want it to be emitted on. If you don't want the channel to be specific to an id your just omit the id from the channel name in your Event and then do something like:
PHP
public function broadcastOn()
{
return new PrivateChannel('users');
}
JS
Echo.private('users')
.listen('SomeEvent', (e) => {
console.log(e);
});
Hope
this helps!

Difficulties with routes translation and api

I am creating an API for my project and I have the following problem:
I'm using the location files.
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get('{contact}', 'WelcomeController#index');
});
This part of code works correctly, and I can access with
http://localhost/project/public/en
However, I am interested in create an API to receive a list of products. So I add this code:
Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get('{contact}', 'WelcomeController#index');
Route::group(['prefix' => 'api'], function() {
Route::get('test', function(){
return response()->json(['foo'=>'bar']);
});
});
});
I don't have any errors, but I can't receive a response. What is the problem?
http://localhost/project/public/en/api/test
UPDATED
I solve part os this problem , in the view I have this
<li><?= trans('locale.allauctions'); ?></li>
This works correctly but I need to put en/anotherurl , how can I put this ?
http://localhost/project/public/api/test is working fine.
This is because your Config::get('app.locale_prefix') is empty! So you defined there is NO prefix. Use dd(Config::get('app.locale_prefix')); to test it.
Why is http://localhost/project/public/en working then?
Because you defined a variable {contact} in
Route::get('{contact}', 'WelcomeController#index');
and en is {contact} then.

How to deal with async requests in Marionette?

I am trying to fill in an ItemView in Marionette with the combined results of 2 API requests.
this.standings = App.request('collection:currentStandings');
this.userInfo = App.request('model:userInfo');
this.standings.each(function(s) {
if (s.currentUser) {
s.set('alias', this.userInfo.alias);
s.set('imageURL', this.userInfo.imageURL);
}
});
userInfoView = new LeagueBar.UserInfo({ collection: this.standings });
The problem is, the combination never happens because the requests have not been fulfilled before I try to combine them.
I know I probably need to add a promise for each request, but I haven't been able to find a clean way to do it. I could make 'collection:currentStandings' and 'model:userInfo' return promises, however, they are currently used in many other parts of the code, so I would have to go back and add .then()s and .done()s all over the code base where they weren't required before.
Any ideas or suggestions?
EDIT:
I have currently solved this in a less-than-ideal way: I created a template/view for the alias and a template/view for the imageURL and kept the template/view for the standings info. This doesn't seem like the best way and I'm interested to know the right way to solve this problem.
here are the two requests I am trying to combine:
Models.CurrentStandings = App.Collection.extend({
model: Models.PlayerStandings,
url: function() { return 'leagues/' + App.state.currentLeague + '/standings'; },
parse: function(standings) {
return _.map(standings, function(s) {
if (s.memberId == App.user.id)
s.currentUser = true;
return s;
});
}
});
App.reqres.setHandler('collection:currentStandings', function() {
weekStandings = new Models.CurrentStandings();
weekStandings.fetch({ success: function(data){ console.log(data); }});
return weekStandings;
});
Models.UserInfo = App.Model.extend({
url: 'users/me'
});
App.reqres.setHandler('model:userInfo', function(options) {
myuser = new Models.UserInfo();
myuser.fetch(options);
return myuser;
});
There are 2 solutions which based on your dependencies among views can be selected:
You can create views which are handling 'change' event of Models.UserInfo and when the data is ready (Change/Reset event raised) re-render the content. It is probably your solution.
If you are looking for a solution which should not create instance of LeageBar.UserInfo until both Models.CurrentStanding and Models.UserInfo are ready, you have to return the result of fetch function, so you may remove calling fetch from setHandlers and use them as following:
this.standings = App.request('collection:currentStandings');
this.userInfo = App.request('model:userInfo');
var that=this;
that.standings.fetch().done(function(){
that.userInfo.fetch().done(function(){
that.standings.each(function(s) {
if (s.currentUser) {
//....
}
});
userInfoView = new LeagueBar.UserInfo({ collection: that.standings });
});

Deferred Promises with AJAX in Angular

I'm trying to send data to my view from an AJAX call to my API. I am able to successfully hit my API and get data, but I was having problems with the view rendering before the AJAX call came back.
I'm trying to wrap my AJAX call in a Promise but it's not working. Here's my layout
Controller
.controller('DashCtrl', function($scope, Tweets) {
$scope.tweets = Tweets.all()
})
Factory doing ajax call
.factory('Tweets', function($http) {
$http.get('http://localhost:3000/tweets')
.success(function(data) {
var tweets = data
debugger
})
return {
all: function() {
//should return the results of the AJAX call when it's complete
}
}
});
I've tried making wrapping the ajax call into a function and using .then(function(payload){ return payload.data }) - Payload.data has my data but its never returned when I call the function. I'm new to angular, so I would appreciate any help or insight.
You should define your factory as
.factory('Tweets', function($http) {
return {
all: function() {
return $http.get('http://localhost:3000/tweets')
.then(function(response) {
return reponse.data;
})
}
}
});
Then change your controller to
.controller('DashCtrl', function($scope, Tweets) {
Tweets.all().then(function(data) {
$scope.tweets = data;
});
})
Use the $resource service. The docs don't mention it, but comments in the source do.
$resolved: true after first server interaction is completed (either with success or rejection), false before that.
So in the controller:
$scope.tweets = $resource('/tweets').query()
And in the view:
<div ng-if="tweets.$resolved">
Loading data with ngResource or from factory promise callback are viable options, but there's one more way nobody mentioned yet: resolve data to controller via route definition. This approach allows to write simplistic controllers that don't know how to load data at all. In most cases it will be more than enough if you don't need to load data dynamically, like pagination or infinite scroll.
You will need to define route and resolve function:
angular
.module('app', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'ctrl',
controllerAs: 'view',
templateUrl: 'view.html',
resolve: {
tweets: function (Tweets) {
return Tweets.all();
}
}
})
})
The tweets property on resolve will inject loaded data into controller as tweets, all you have to do is just assign received data:
.controller('ctrl', function (tweets) {
this.tweets = tweets;
});
In addition, here's how Tweets service might look like:
.factory('Tweets', function ($timeout) {
function all () {
return $timeout(function () {
return ["hey", "there"];
});
}
return {
all: all
};
})
Basically, it exposes methods that return promise, returning some data ($timeout returns promise too, so I've used it instead of $http for example purpose).
Full example on JS Bin.

Resources