Support the ongoing development of Laravel.io →
posted 9 years ago
Configuration
Last updated 1 year ago.
0

Just curious why angular? I like jquery.

0

jimgwhit said:

Just curious why angular? I like jquery.

You know Angular and jQuery are totally different things, right? One of them is a library and the other one is a framework. It is like comparing an XML parser with Laravel framework.

0

For the question:

If AngularJS application is working when you go to /, redirect missing paths to /. AngularJS will be loaded. Then it will check the page url and will load the related page.

So, use:

//This will redirect all missing routes to AngularJS Framework .
App::missing(
  function ($exception) {
    return redirect('/');
  }
);
0

Not sure if I fully understand your question.

You have said it's a SPA, so why would you still need Laravel to do routes (or missing routes)? Should not it be something like this using otherwise in Angular?

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }]);
Last updated 9 years ago.
0

AngularJS, commonly referred to as Angular, is an open-source web application framework maintained by Google and a community of individual developers and corporations to address many of the challenges encountered in developing single-page applications. Its goal is to simplify both development and testing of such applications by providing a framework for client-side model–view–controller (MVC) architecture, along with components commonly used in rich Internet applications.
Sorry I didn't know you were doing a simple single page application.
So again why aren't you using jquery in laravel?

Last updated 9 years ago.
0

awsp said:

Not sure if I fully understand your question.

You have said it's a SPA, so why would you still need Laravel to do routes (or missing routes)? Should not it be something like this using otherwise in Angular?

The reason is that: If you go to the site.com/ Angular will be loaded. If you click a link and go to the site.com/page for example, angular will load this page. Until here everything works as expected.

But if you just type site.com/page to your browser's address bar, you are actually sending a request to site.com/page. As it doesn't exist, web server will return a 404 and angular application will not work. Angular even didn't get a chance to be loaded. In order to prevent that problem, web server should redirect those 'virtual' angularjs paths to somewhere that angularjs can be loaded, for example site.com/. After that, angular will handle the rest.

Last updated 9 years ago.
0

I think I know what you mean now. But still I don't think you need Laravel to do missing routes.

First of all, when handling HTTP responses including 404s, it is server responsibility, not Laravel's. The reason why Laravel can handle missing pages, is because .htaccess redirects all HTTP requests back to index.php.

So to my understanding, indeed you don't really need Laravel to handling missing pages. Because you can do the same thing like Laravel to Angular. Redirect all traffics to index.html, your angular application.

Personally, I don't recommend mixing the 2 technologies. As Laravel is doing request-response base compare to SPA which normally handles routes on client base in terms of page loading in frontend. But yeah, it's totally up to you. :D

erkanarslan said:

awsp said:

Not sure if I fully understand your question.

You have said it's a SPA, so why would you still need Laravel to do routes (or missing routes)? Should not it be something like this using otherwise in Angular?

The reason is that: If you go to the site.com/ Angular will be loaded. If you click a link and go to the site.com/page for example, angular will load this page. Until here everything works as expected.

But if you just type site.com/page to your browser's address bar, you are actually sending a request to site.com/page. As it doesn't exist, web server will return a 404 and angular application will not work. Angular even didn't get a chance to be loaded. In order to prevent that problem, web server should redirect those 'virtual' angularjs paths to somewhere that angularjs can be loaded, for example site.com/. After that, angular will handle the rest.

Last updated 9 years ago.
0

Edit the render function in the app/Exceptions/Handler.php file:

    public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

        // handle Angular routes when accessed directly from the browser without the need of the '#'
        if ($e instanceof NotFoundHttpException) {

            $url = parse_url($request->url());

            $angular_url = $url['scheme'] . '://' . $url['host'] . '/#' . $url['path'];

            return response()->redirectTo($angular_url);
        }


        return parent::render($request, $e);
    }
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

cskiwi cskiwi Joined 23 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.