Use Laravel internals in another php website - php

I have an interesting question regarding Laravel.
I am building a control panel with certain functionality with Laravel 5.
This control panel will work standalone in a subdirectory of any another PHP website.
Here is how the folders tree looks like:
/index.php
/(other php files from the main project)
/controlpanel (Laravel)
I want to use the Laravel functionality within the main PHP project.
For example I want to be able to query the database of the control panel with Eloquent models from within /index.php.
Something like limited Laravel (without routers, etc).
I have tried to include the bootstrap/autoload.php and call Eloquent model, but the IoC Container is not instantiated and it's not working.
I hope someone can point me to the right direction.

You can use some of Laravel's packages outside the framework, however it takes some doing in most cases.
Laravel Illuminate Router Package In Your Application
Use Eloquent Outside of Laravel
The resources above are for Laravel 4, but you could use them as a starting point as they might be very similar to the approach needed for Laravel 5 packages.

Related

Lumen project structure

I'm beginner at Lumen (and php) and I am a bit confused all the structure as the only people I talked about Lumen to were interns just like me. I created a project where I use Models where all my methods and functions are, I have constructor in my controller class where I call functions from class Model.php and then pass that to view.blade.php (in resources folder) where all my html goes. I return view from controller. My question, is that correct? Should I have class for view? If my structure is not correct, how should it look? What correct sequence of passing information should be then?
Thank you in advance for claryfing it for me :)
I know MVC structure should be similar to .NET but still I am confused somehow.
You should not use Lumen for a fully-fledged application with HTML views, assets, etcetera.
Lumen is a microframework specifically designed to build APIs in a lightweight environment, which allows you to focus entirely on the resources to serve, instead of the design.
Lumen is way faster compared to Laravel, for example the Routing is handled by FastRoute instead of Illuminate Routing, which is quicker and lighter for the application.
Use Laravel for what you are trying to do.

Laravel 4.2 controllers in subfolders

I am making a controller for each type of request post/put/get.
So my question now is, what is the best way to put controllers in subfolders when using L 4.2 ?
/controllers/subfolders..
I've seen some people using namespacing and some people simply makes a subfolder and put their controllers in it then run composer dump autoload-
But is there any "best practice" way to do it in L 4.2?
I do it the namespace way. One advantage this gives is that we can have same named classes inside the folders. Currently in a Laravel 4.2 app that I'm building, I am using controllers/api subfolder, with the namespace of Api for all the classes in it. And one of the classes is UsersController. Which might also be used for the frontend website, so now you will have to say FrontendUsersController, or something weird and long. So to avoid this, better get in shape with namespaced controllers.
Also, Laravel 5 advocates namespacing for your project, so does PSR standard. So this is probably much better way in the long run.

View and controllers on different machines in Laravel

I have got a requirement from a customer that my web UI should not have any direct access to my database. My web app is built in php using the Laravel framework and Eloquent. One option is rewriting the code in my controller and model into a webservice (in any other language be it php or java) and calling the webservice from the UI, but this will involve tremendous work. Is there any way in Laravel where I can move my controller and model to another server and calling the controller from UI via Laravel routes? The view must be on one machine and the model-controller on another machine.
To the best of my knowledge this cannot work. Although Laravel is highly customisable, the work involved in moving your entire application to a new server while keeping the view where they are is extremely too much and possibly won't work.
Your suggestion to use web services is something to consider but in reality you will be creating a static website in one server which uses API's to talk to Laravel on a other servers. They will not be views and definitely not in blade templates. Remember that you have other UI related dependencies in vendor and public directories.
My suggestion is to have the DB on a separate server which you can connect to by editing the config files - this is a very normal setup.
Then if you want you can create some web pages which use the API to talk to Laravel from another server but again I cannot see why this would be needed as the views are still not accessible by anyone other than the server.
Please let me know if I'm missing something :)

Generating nested resource with same directory structure in laravel 4

Sorry if I am asking a silly question here, but I have googled it a lot and couldn't find any satisfactory answer.
I have some experience with codeigniter and I am new to laravel.
I have just installed laravel and jeffrey way generator tool. I am first working on my back-end admin panel, so what I need is to create nested resources so it will map to domain.com/admin/SomeController
But while using php artisan generate:resource country --fields="name:string, status:boolean"
It is working fine. but generating controllers, models, views etc in respective root directory and controller name should also be changed to support nested controllers
I want to create all these in some meaningful directory structure
App/controllers/admin/AdminCountryController.php
App/models/admin/Country.php
App/views/admin/country/index.blade.php etc.
I also checked generator tool documentation but couldn't find these details.
So I want to know how can it be achieved using generator or I have to do it manually.
If I chosen wrong path please tell me as I am at very initial phase of my project and my application structure will depend on it.
You need to create first that directory. For example, you want a controller app/controller/admin/AdminController.php, first create the admin folder on app/controller, then you can do php artisan generate:controller admin/adminController.php.
I am not sure if this coulde be done using Resouce.
Also routing in laravel is different with routing in CI. CI depends on domain.com/controllerClass/method/args and laravel is different.

Using Laravel 4 for a subproject of a larger existing project

I am starting a small project that I think might be well suited for Laravel 4. However, the project would have to coexist with a much larger existing legacy project, one that has its own bootstrapping system and routing (via htaccess). Is there a good way to have Laravel 4 coexist with an existing project in this way?
The way you normally start a Laravel 4 project basically takes over the entire project directory. That wouldn't be suitable for our current legacy project, which has many of the php scripts right in the public_html directory. Perhaps the entire Laravel project could be installed in a subdirectory of the main project, but that seems like a fairly messy solution. Is there a better way?
Yes, Laravel 4 components can be used without installing or using the full Laravel 4 framework - it's really flexible and pieces can be swapped out.
I would use Namespaced Controllers and Models, map your legacy code to them and slowly migrate over as alot of Laravel 4 can be used individually in external projects.
Eventually you can move over the routing to the Laravel 4 or any other Router you choose.
I've used the Database Layer in a Codeigniter project without any problems.
The only thing you need to be careful of is the PHP version as it relies on mcrypt, but if your're not using any of the password features I think you'll be okay.

Categories