codeigniter 4 enable users to define their own routes - codeigniter

I've started with Codeigniter 4 today. I have an app that I've built with Codeigniter 3 and now I want to upgrade it to CI4. The first thing is to upgrade routes. I have a simple but GREAT solution by enabling users to define their own routes with a simple page, like this:
And it was super easy, just needed to load and update Routes.php file - arrays of routes.
Now, as I am looking at app/config/Routes.php in CI4 I see it is not a simple array but there is much more to it. So, my question is what would be the simplest way to enable users to define their own routes with a webpage?

Related

URL Routing: Laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

how to connect two projects Laravel 4.2 and laravel 5.5?

i have a laravel 4.2 project that i want to connect it to laravel 5.5 project
Q1) how to login in the first and and authenticated to the second to so i don't have to login to the second
Q2)how to connect them to the same DB
Q3)let's say i'm in page X in laravel 4.2 and i want to direct it to Y in laravel 5.5 where i should write the route in laravel 4.2 or in 5.5 ?
at the end both of the projects are going to be on the same server and have the same domain if that is going to be helpful
Q1) how to login in the first and and authenticated to the second to so i don't have to login to the second
if the 2 webapp are in the same domain, it should not be an issue
especially if they use the same database.
Q2)how to connect them to the same DB
just use the same credentials and save it on each env file.
Q3)let's say i'm in page X in laravel 4.2 and i want to direct it to Y in laravel 5.5 where i should write the route in laravel 4.2 or in 5.5 ?
you can use the function route instead use the manual routing like
to('somepage-on-theother-laravel') this is a tedious work. So i want
to ask if why are you using the 2 webapp in the same domain instead of
just using 1 webapp framework. Im thinking that this laravel 4.2 is
the existing one and you're create a new one and you want to use the
laravel 5.5 right?
This is easy to do, just put both applications on the same server and link them using the same credentials in the .ENV file, don't forget you will also need to create Models in both apps so that both apps can use the tables.
As for the routes, you would need to add the routes & functions that can be called in each individual app.
So if the user clicks the URL for /link-to-4.2 you should have a route in 4.2:
Route::get('/link-to-4.2', 'index#controller');
and then in your 5.5 you should have a route for any URLs that direct to anything there. i.e: /link-to-5.5
Route::get('/link-to-5.5', 'index#controller');
this all being said, what is the justification for using 2 apps? I think there could be a number of security issues that might cause headaches, this will also likely be a real nightmare to manage moving forward.
I would strongely suggest that you just upgrade the 4.2 app to be 5.5.

laravel 5.4, where to put controllers, models, views and routes?

Having gone through numerous laravel tutorials and examples, i have seen source code with controllers, models and views in very different directories.
The question is, where should they go, and how does the system (which presumably requires convention) work if they are put in a different place?
E.g. the official laravel quick-start project puts them in:
app/Http/Controllers/xxxControler.php
app/Http/routes.php
resources/views/xxx.blade.php
Cant find where it has put the models.
However, the simple laravel crud master tutorial puts them here:
app/controllers/xxxControler.php
app/models/xxx.php
app/routes.php
app/views/xxx/create.blade.php
app/views/xxx/edit.blade.php
Controllers
If you look in the 5.4 documentation here it says controllers should go in app/Http/Controllers.
If this is the case, how did the latter example work (as the controller files are in the "wrong" dir, and I assume that laravel, like grails, relies on convention over configuration)?
The latter example directories seem much more logical than the official place, as a controller has nothing to do with Http - its part of the app logic.
Models
The official documentation mentions that "models typically live in the app directory". This implies they can be put put in app/models, which is good.
views
Unfortunately, the official documentation says they have to go in resources/views. Again, this would be far more logical to have views, controllers and models all being together as sub directories of app, as per the second example above. Is it possible, and advisable, to use this logical, but unofficial structure? as a beginner, I have difficulty in finding the models, views and controllers as they are put in 3 different directory paths with no obvious logic.
routes
The official documentation says that routes should go in routes/web.php. I have not found an example project using this convention - I have seen them in app/routes.php, and app/http/routes.php. Assuming its ok to put them in random places under app, how does one configure where the routes definition files go?
For Laravel version ^5.4
Controllers should be in this directory
app/Http/Controllers/xxxController.php
Models should be in this directory
app/xxx.php
Views should be in this directory
resources/views/xxx.blade.php
Routes should be in this directory
routes/web.php // for web routes
routes/api.php // for api routes
These are all recommended places to put controllers, models, views and routes. You are free to modify everything of course!
Older versions of Laravel have different places for Controllers, Models, Views and Routes. Check the documentation where they should be.
The thing is that you must recognize the Laravel version because Laravel went through many changes between years 2011 and 2016...
That link in your question for example is from year 2013 which is very old.
And I strongly recommend you to follow the Laracasts tutorials at laracasts.com

Run two websites with one laravel installation

How we can use multiple applications with one Laravel installation.
For example:
www.apple.com
www.orange.com
both website will use ONE CORE laravel installation with separate database and all other separate stuff.
Is that possible?
If yes then how?
Also I have done the same thing for CodeIgniter but not sure how i can do with
laravel.
You can use route group for this, it's something like this:
Route::group(['domain' => 'myapp.com'], function () {
Route::get('user/{id}', 'Controller#Method');
});
Official documentation: Sub-domain routing
You can use this package https://github.com/nWidart/laravel-modules to better organize your code into it's separate websites within the same laravel base code. E.g. you can have Website1 and Website2 modules that has their own Controllers, Models, Views, database migrations, etc.

gradually migrate application from codeigniter to Laravel

I have a quite large application written in Codeigniter 1.7.
It needs some significant enhancements and I have been evaluating Laravel 4 as the PHP framework.
I am trying to find out if anyone has successfully extended an existing Codeigniter application using Laravel so that some screens are Laravel and some are Codeigniter.
I imagine that authentication (currently using Ion Auth) and Session management (using mysql backend) would be two major hurdles to overcome.
If this is an option then I will explore in more detail as the amount of legacy code needing transferring is considerable.
Thanks in advance.
I suppose in your Laravel routes file, create a route for every page you have written in CodeIgniter. Route everything to the URL that CodeIgniter is expecting and as you complete portions in Laravel, change the routes to point to the correct ones Laravel expects as you go.
Before you do anything though, I'd get everything you need for authentication finished in Laravel, turn authentication off in CodeIgniter, and just let Laravel handle that in the routes.

Resources