D
D
Demid Borodin2014-12-14 19:05:52
Angular
Demid Borodin, 2014-12-14 19:05:52

Angular + Laravel Get Request Validation?

There is a small application in Angular + Laravel.
There is an Angular application config

crmApp.config(['$routeProvider', '$locationProvider',
    function ($routeProvider, $locationProvider) {
        $locationProvider.html5Mode(true);

        $routeProvider
            .when('/', {
                controller: 'CrmIndexController',
                templateUrl: 'templates/crmIndex.html'
            })
            .when('/workpeople', {
                controller: 'CrmWorkPeopleIndexController',
                templateUrl: 'templates/workpeopleIndex.tpl.html'
            })
            .when('/workpeople/show/id/:workerId', {
                controller: 'CrmWorkPeopleShowController',
                templateUrl: './templates/workpeople-show.tpl.html'
            })
            .when('/workpeople/edit', {
                controller: 'CrmWorkPeopleEditController',
                templateUrl: './templates/workpeopleEdit.html'
            })
            .when('/workpeople/delete', {
                controller: 'CrmWorkPeopleDeleteController',
                templateUrl: './templates/workpeopleDelete.html'
            })
            .when('/map', {
                controller: 'CrmMapController',
                templateUrl: './templates/mapIndex.html'
            }).otherwise({
                redirectTo: '/'
            });

    }]);

As you can see, $routeParams is received in one of the routes.
Next, we pass it to one of the controllers, where it is used to compose a query string for the "API" of my application.
crmApp.controller('CrmWorkPeopleShowController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {

    $scope.watispage = 'Сотрудник';

    $scope.workerId = "api/workpeople/id/" + $routeParams.workerId;

    $http.get($scope.workerId).then(
        function (result) {
            $scope.workpeople = result.data;
        }
    )
}]);

The actual ID in the database is of type INT and only INT.
That is, with a request like:
/workpeople/show/id/2132132 - the route should work and send a request to our ID.
And when requesting (for example):
/workpeople/show/id/dsad123 - a route with a redirect to the main page of the application should be executed and, most importantly, do not make a GET request to our IP itself.
What do you think is the best way to implement this feature?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Gushchin, 2014-12-14
@demidborodin

Try adding a resolve to the route:

resolve: {
    validation: function ($q, $routeParams) {
        var deferred = $q.defer(),
            id = parseInt($routeParams.id, 10);

        if (!isNaN(id)) {
            deferred.resolve();
        } else {
            deferred.reject('VALIDATION FAILED');
            $location.path('/login');
        }

        return deferred.promise;
    }
}

T
Terminaft, 2014-12-14
@Terminaft

1) Validation is needed on the side of Angular and Laravel at the same time
2) On the side of Angular, you can simply not consider the path 'workpeople/show/id/dsad123' as declared in the config

.when('/workpeople/show/id/{workerId:\d+}', {
                ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question