laravel setup different route according to domain/subdomain - php

good day,
I am using Laravel to build both website and APIs for mobile backend, my API controller is setup like this:
Route::controller("api/", "ApiController"); //mobile APIs, response is in JSON
and other routes and like this:
Route::controller("/", "MainController");
I need to setup a subdomain for the APIs, for instance:
http://api.mysite.com
to route directly to API controller, I need that because I don't want the URL to get long and ugly. Any suggestions ?

Try using this
Route::group(array('domain' => 'api.mysite.com'), 'ApiController');

You can use route grouping for this. Just point the subdomain to the same location as the normal domain and add a route group.
More information is available in the docs.

Related

Laravel API with Google Analytics

I made a REST API with Laravel, basically I get they route pass the route to the controller and from the controller I get the data and return a json formatted response, I was wondering If there is any way to add Google Analytics tracking to this.
Thanks.
Your best bet seems to be the Google Analytics Measurement Protocol:
https://developers.google.com/analytics/devguides/collection/protocol/v1/
Also, since Laravel has the ability to run packages, it appears someone has already created an appropriate package for Laravel:
https://github.com/irazasyed/laravel-gamp
Note - I have not tried this package so I can't say whether it will save time over implementing the calls directly according to the GAMP docs.

Need Help About Laravel Api

I have two web Application that's work in different domaine , The first one is a Laravel web application like this
domaine1.com
and another web application that's built also with laravel
domaine2.com
and i have on the first application a dashboard like this ( domaine1.com/dashboard) ,
I want to add from this dashboard for example A books to the the web Application 2 , that's have it own database and tables ......
Please can someone tell me how can i do it ?
i'm kind of Newbie on laravel , i think i should Use something like API ? or Something else ?
Create a route in your Application 2 routes.php to get the books
for example
Route::get('domaine2.com/books', 'BookController#getBooks');
In your BookController:
public function getBooks()
{
$books = Books::all(); //I assume that Books is your model
return $books
}
Now all you have to do is call this in a function in your controller from domaine1.com application to have your books
$books = file_get_contents('http://domaine2.com/books');
To give it a try first and be sure it works try accessing http://domaine2.com/books to see if you get a json with your books.
Yes. You should use API. Although it will not be that easy, because you want to create cross-domain requests.
If data you want to pass is not classified, you can make a public API handler, meaning that everyone will be allowed to access this. This can be for example a get request that return some objects from your database. Let's say you create a route in your domain1.com:
GET domain1.com/api/books that returns json/data
Then if anyone visits http://domain1.com/api/books he will see this response formatted in json. You can utilize it in your domain2.com app using CURL or built in vue.js with axios.
If data you want to provide is classified or you want to make requests other than GET (POST for example) you will have to read about application authorization. Putting in a simple way: you will have to show your domain1.com app that someone who wants to access restricted data is allowed to do it.
By the way, mentioning other response: you should utilize api.php, not web.php in routes. And have in mind that file_get_contents is significantly slower than CURL.

How do I use Laravel with AngularJS?

I am trying to use Laravel with AngularJS and want to understand the best way to set up such a project.
Should I (A) have a single domain and consume an API from the Laravel project, or (B) have website.com and api.website.com and consume the API as if it were a 3rd party API?
I can see pros and cons for each, but what I can't get my head around is how routing would work with option A. I assume the initial routing would be via Laravel to display a top level view and then from that point onwards AngularJS would do the routing, but surely AngularJS and its routing are only initialized when the page loads. For example, if a user goes to a subroute without hitting the site root, no route on the Laravel side will exists for that and thus would it not respond with 404/Not Found?
What is the best setup for consuming my Laravel API within AngularJS?
I suggest to separate (your option b).
Front and back-end are totally separated
You can replace one of them without problems in the other
Use middleware, json responses and http status codes
Use a framework for back-end too (for example Laravel or Lumen)
About routes ...
Your back-end has its own routes (endpoints).
Your front-end has its own routes (totally different), but should send GET/POST/PUT etc. requests to the back-end. The back-end returns (json) response, which will be parsed by the front-end.
Develop both separately! So you can use the back-end for third party later.

Laravel URL Architecture

I am building a social networking application using Laravel 5, and I am slightly confused about the structure of the URLs.
Using a previous Stack Overflow post from another member, I have managed to grasp the basics (profiling, account, etc). Here is what I have so far:
http://myapp.com/account/create
http://myapp.com/account/login
http://myapp.com/account/logout
http://myapp.com/account/verify
http://myapp.com/account/verify/{token}
http://myapp.com/account/settings
http://myapp.com/profile/FooMan
And that's all well and good (at least I think so - any tips are welcome), but how do I go about creating a URL structure for things such as friends? I get that you could have:
http://myapp.com/profile/FooMan/friends
But to me, that seems ugly. And if I wanted to add a "/add" route to the friends, how would I go about that also? Thank you in advance!
P.S. On some of the pages, such as "login" and "create" I use the route::get() to display the form, and route::post() to submit the details to the controller. Is this bad practice?
I think following a standard system such as facebook or twitter would help with your routing approach.
To avoid lengthy urls, you can route directly to a profile via their user name, using a route such as:
Route::get('{username}', ...);
Route::get('{username}/friends', ...);
// etc
// Current user
Route::get('friends', ...);
Now, there will be occasions where a username may conflict with a core action such as domain.com/friends or domain.com/login. Establish a list of core actions and restrict users from signing up with those keywords. This is simple by using laravel's not in validation.

Laravel 5 website and API using the same app

The application I am migrating into Laravel 5 is a website and its API.
The API is used for the mobile apps and for the website to pull the data.
The website will not be developed to be a Single Page App, because I already have all the views and I'm just migrating the website to Laravel 5.
How can I do the following without duplicating my code?
For instance, allow /products to list all my products using this API endpoint (/api/2.0/products).
The same applies to all other routes
Yes you can. I am currently doing this myself. Writing the base API first and then dogfooding it for my own website. There is a good laravel package called Dingo ( https://github.com/dingo/api ) that can give different output depending on where you call it from.
If you call it simply from api.yourwebsite.com/products you will get JSON. but if you call it internally like API::get('/products') you will get array/object whatever you're returning instead.
So I have a website that is using the API to render itself. This way, you only write the API once, and can use it for your frontend website, mobile, or give it to third party developers too.
I hope this answers your question. If you have any more questions, please let me know. Thanks
You can do it by creating repositories for your code.
Then create separate controllers for both APIs and Website. Through that your code doesn't duplicated and you can access that repository from both the controllers and return response accordingly.

Categories