I have a Laravel App and a domain to access it. The Domain points to the public folder and executes the '/' route which then executes a certain method 'BaseController#index' in a controller.Now I want to add another domain. But this domain should execute another method 'AppController#run' (route can be '/app/run/.
How can I achieve this?
I created a vhost for this other domain that points directly to public/app/run. This works but the browser shows domain.com/app/run which I don't like.
So I think what I have do do is let this domain point to public and then in my routes file say that this domain shell execute 'AppController#run'
Or in the worst case it points to the '/' route and then inside the BaseController#index method I have to check what domain is accessing. But this seems not good to me.
Any ideas? I wonder why I can't find a lot on Google since this should not be only important to me.
First, all vhosts should have set the document root to the public public directory, otherwise Laravel won't bootstrap correctly.
Then you can add specific routes for that domain. For example:
Route::group(['domain' => 'seconddomain.com'], function(){
Route::get('/', 'AppController#run');
});
Now if you go to seconddomain.com run() in AppController will be called
Laravel Framework 5.7.2
I wanted a separate domain for my API.
My solution was to edit the mapApiRoutes() in Providers/RouteServiceProvider.php
protected function mapApiRoutes()
{
//This is what we're changing to Route:domain('www.sub.domain.com')
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
and the function should look something like this:
protected function mapApiRoutes()
{
Route::domain('www.sub.domain.com')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
You will also need to reconfigure your apache depending on what you're trying to accomplish.
Hope this helps.
Related
I tried to understand how the routing in CodeIgniter work.
I want to use normal access to sides which are not a kind of user area or something special - only normals links in the main root of the website.
So I try this.
I've set in the routes.php
$route['/'] = "index";
I've created controller names Frontend.php and a model named Frontend_modell.php
The Controller (nothing to do)
public function index()
{
redirect(site_url('index'));
}
The Modell
public function __construct()
{
parent::__construct();
}
I've added an index.php inside the view-folder Frontend and I add the index.php (for test) in the main folder from appilation and in the view folder.
If I try to access www.domain.com I see the URL will change to www.domain.com/index, but no side will come up. "the page you requested was not found."
What I do wrong ? I hope somebody can explain to me how it works correctly and why.
First of all routes uses controller_name/method_name and here you tried to use method_name directly.
Secondly, most of cases you don't need to play with routes unless you need a special handler or rerouting, so mostly keep it to default.
Btw the url rerouted to www.domain.com/index cause it looked for a controller named index as you specified and there in no controller found with that name it should be frontend if that's your controller.
The reason for the problem was from another point.
The Controller i've create had some bugs, after checking the function of the CR Controller, the routing works fine :-))
I am new to laravel,I had wrote api route code to register controller:
Route::post('test','Api\Auth\RegisterController#index');
In Register controller i had written simple code
public function index(Request $request)
{
return 'hello';
}
I am getting the output in postman like:
Sorry, the page you are looking for could not be found.
not hello.
Here the images:
1 3
Routes defined in the routes/api.php file are nested within a route
group by the RouteServiceProvider. Within this group, the /api URI
prefix is automatically applied so you do not need to manually apply
it to every route in the file.
You are trying to make a request to a route which does not exist.
In Postman
Change:
http://localhost:8080/App/api/test
To:
http://localhost:8080/api/test
You're telling laravel to route assoaciate '/api/test', not '/App/api/test', which is the adress you're trying to reach.
Also, if you plan to reach that address straight from the location bar of your browser, you should register the 'GET' method as well.
As you are returning something in function you need to use route as get() instead of post().
Route::get('test','Api\Auth\RegisterController#index');
I can't seem to find an answer on the web or maybe I'm missing something.
Let's say my laravel application domain is my-laravel.com
Now I want to make requests at api.my-laravel.com which will work just same as my-laravel.com/api/
How do I do that?
Solution: Changed prefix('api') to domain('api.my-laravel.com') in RouteServiceProvider#mapApiRoutes
You may group your routes in a sub-domain, change your routes/api.php
Route::group(["domain" => "api.domain.key"], function() {
// your api routes.
});
Edit:
Check your RouteServiceProvider then remove the api prefix in mapApiRoutes method.
Alternatively(Except DNS and mod_rewrite)
Sub-Domain Routing
Route groups may also be used to handle sub-domain routing. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified by calling the the domain method before defining the group:
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
Source: https://laravel.com/docs/5.4/routing#route-group-sub-domain-routing
Note:
Before this map your subdomain to point your server and point your domain (laravel.com)
Laravel Problem: I want to unset particular session on every controller except one(specific) controller.
I am thinking of helper class. But if is there better way (like middle ware).
If is anyone have idea, please share.
Middleware is a good idea, you can use easily with your routes:
Route::group(['middleware' => 'clearSession'], function () {
// .. your controllers
});
// A controller where not clear
Route::controller('mycontroller', 'MyController');
But you can also add to your controllers constructor, and you don't have to mess with current routes, route groups.
Say I have a controller, "Articles" but I want it to appear as a sub-folder (e.g. "blog/articles"), I can add a route like this:
$route['blog/articles'] = 'articles';
$route['blog/articles/(:any)'] = 'articles/$1';
This works fine, the only problem now is that example.com/articles and example.com/blog/articles both use the Articles controller and thus resolve to the same content. Is there a way to prevent this?
To add a little more clarity in case people aren't understanding:
In this example, I don't have a 'blog' controller, but I want 'articles' etc to appear to be in that subfolder (it's an organization thing).
I could have a blog controller with an 'articles' function, but I'm likely to have a bunch of 'subcontrollers' and want to separate the functionality (otherwise I could end up with 30+ functions for separate entities in the blog controller).
I want example.com/articles to return a 404 since that is not the correct URL, example.com/blog/articles is.
Shove this in your controller:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR show_404();
}
You can use subfolders in Codeigniter controllers, so in CI, the following directory structure works:
application/controllers/blog/articles.php and is then accessed at
http://example.com/blog/articles/*.
If, for some reason, you're set on routing instead of accessing the controllers in folders (you want to have a blog controller, for example, and don't want to route to it), you can do as suggested above and add the test for 'blog' to the constructor.
If you're in PHP5, you can use the constructor function like this:
function __construct()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
or, in PHP4, like this:
function Articles()
{
parent::Controller();
$this->uri->uri_segment(1) == 'blog' OR redirect('/blog/articles');
}
I would suggest using redirect('blog/articles') instead of show_404(), though, so that you're directing users who hit /articles to the correct location, instead of just showing them a 404 page.
Routing there does not mean it will use a different controller, it just creates alias url segment to same controller. The way will be to create another controller if you are looking to use a different controller for those url segments.
If both /blog/ and /articles/ use the same controller, you can reroute one of them to a different one by just adding a new rule in your routes file.