Laravel 5 - Define custom route method? - laravel-5

I just got an issue , i have 2 problems :
I want create a custom route for fast using without copy past code many time. Example Laravel 5 have default Route:resource (...) to make Restful! But i want to make my custom route function , Route:api(...) , Route:xxx(...) ... and I can custom it what I want !
How can I use multi route file ? Example : I can define route in App\User\route.user.php , App\Book\route.book.php .... because now, I can only use route file in route folder default !

I do not understand properly question 1. But for question 2, try this:
Go to app/Providers/RouteServiceProvider.php. Look for the function mapWebRoutes(). The line
require base_path('routes/web.php');
Duplicate it and change so you now have :
require base_path('routes/web.php');
require base_path('app/User/route.user.php');
require base_path('app/Whatever/route.whatever.php');
And laravel will load all routes within those files. Now, I've tested this, it works (Laravel 5.3) but I can't guarantee anything or if there are going to be conflicts with routes (duplicates). But yeah, it works.

Related

Laravel: Creating route resource for sub directory

I am new to laravel and creating a spare parts maintenance app.
I created a route resource for spare parts using :
Route::resource('/parts' , 'SparePartsController');
This works fine.
Later I wanted to also create another route resource for spare parts categories. So I created the controller and used:
Route::resource('/parts/categories' , 'SpCategoriesController');
But this second resource wont work. When i go to www.myapp.com/parts/categories , I get a blank page. Any idea whats wrong?
Try to group the routes with a prefix for example:
Route::group(['prefix' => 'parts'], function(){
Route::get('/', 'SparePartsController');
Route::get('/categories', 'SpCategoriesController');
});
This will route all traffic from /parts to the SparePartsController and /parts/categories will call the SpCategoriesController
Look at the documentation for more information:
https://laravel.com/docs/5.3/routing#route-groups
If you want to call a specific function of the Controller just write:
Route::get('/', 'SparePartsController#functionName')
The SpCategoriesController resource route won't work because SparePartsController resource route is taking precedence over it.
To fix that, place your routes in this order:
Route::resource('/parts/categories' , 'SpCategoriesController');
Route::resource('/parts' , 'SparePartsController')
Ref: https://laravel.com/docs/5.3/controllers#restful-supplementing-resource-controllers

laravel routing based on convention

I'm trying to setup a simple routing system based on convention.
My app will have this structure
Http
--Controllers
----Admin
------User.php
----Books
------Add.php
----etc...
I want to be able to add new Folders and controllers without adding routes manually to the web.php file.
For example I want the route to respond to /Admin/User URL with User.php controller.
I'm trying something like this, but I don't understand how to write the internal router...
Route::any('/{module}/{action?}', function($module, $action = 'index') {
Route::get('*',$module.'\'.$action.'#index' );
});
It seems that Rout:get('*'... never matches.
PS the controller namespace is correct and I reloaded with composer.
The controller works if called harcoded.
I tried also to escape '\'
$r=$module.'\\'.$action.'\\'.$action.'Ctl#index';
Route::get('/',$r );
But no result. The route is intercepted but nothing i served
It seems I came up with this
Route::get('/{module}/{action}', function($module,$action) {
return App::make('\App\Http\Controllers\\'
.$module.'\\'.$action)->callAction('index', []);
});
Any other better way?

Laravel Override View::make namespace, cashier specific

How would I override the View::make('cashier::receipt'); view so that when that particular namespace is called like that, it checks my folder first and then defaults back to the vendor path.
View::addNamespace('cashier', [
'/path/to/my/views', // check first
'/path/to/original/views' // check second
]);
I believe that's how Laravel handles custom views for packages already. https://laravel.com/docs/5.2/packages#views - see Overriding Package Views here.
Laravel registers two locations to load views so they can be easily customised, the standard vendor path and something customisable.
Laravel will first check if a custom version of the view has been provided by you, for example in
resources/views/vendor/cashier.
Let me know if there was something more specific you were trying to achieve but I believe the info there will get you going.

Laravel: How to find the right blade master template?

To extend a blade template you have to write
#extends('folder.template_name')
This works for standard installation.
I've created a module for the backend and now I can't use my module template because Laravel catches the first record and that is the standard view folder.
My structure looks like this:
app
-- modules
-- modules\backend
-- modules\backend\views
-- modules\backend\views\layouts\master.blade.php
-- views
-- views\layouts\master.blade.php
So when I'm in the backend and try to display my template:
// app\modules\backend\views\page\index.blade.php
#extends('layouts.master')
Laravel renders the app\views\layouts\master.blade.php instead of
app\modules\backend\views\layouts\master.blade.php
I've tried many names inside that #extends e.g.
#extends('app\modules\backend\views\layouts\master')
#extends('app.modules.backend.views.layouts.master')
#extends(base_path(). '\app\modules\backend\views\\' . 'layouts.master')
Nothing works.
While using a package or autoloaded module, referring to it's resources is done using the double colon notation. In your case, to access the module's master template you need to use
#extends('backend::layouts.master')
These conventions are described in the docs, for further info please refer to
Laravel 4 package conventions
Make sure /app/config/view.php has a path entry for where those views are located.
I.E.
'paths' => array(__DIR__.'/../views'),
To
'paths' => array(
__DIR__.'/../views',
__DIR__.'/../modules/backend/views'
),
or whatever represents your actual path.
From here you might want to look into doing the view folder loading via another mechanism if your views are in dynamically generated folders. Maybe a module::boot event that adds the module path to the view paths array? Just an idea.

SEO-friendly URLs in CodeIgniter without the use of slugs?

Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Access from the SEO-friendly URL should be allowed, but access via the integer should cause a 404 error.
This should also work dynamically, where any integer is automatically converted to a URL-safe version of its corresponding category name.
I've seen a few solutions that use 'slugs'... is there a decent alternative?
Thanks.
I've only been working with CodeIgniter for the past couple of months in my spare time, so I'm still learning, but I'll take a shot at this (be gentle):
There is a url_title() function in the URL Helper (which will need loaded, of course) that will change Foo Bar to foo-bar.
$name = 'Foo Bar';
$seo_name = url_title($name, TRUE);
// Produces: foo-bar
http://codeigniter.com/user_guide/helpers/url_helper.html
The URL helper strips illegal characters and throws in the hyphens by default (underscores, by parameter) and the TRUE parameter will lowercase everything.
To solve your problem, I suppose you could either do a foreach statement in your routes.php file or pass the url_title() value to the URL, rather than the ID, and modify your code to match the url_title() value with its category name in the DB.
Afaik the link between 4 and "foo-bar" has to be stored in the DB, so you'll have to run some queries. This is usually not done via routing in CI. Also routing just points a URL to a controller and function and has little to do with url rewriting.
Why don't you want to use slugs?
You could try storing the search engine friendly route in the database using this method or this one.
I wouldn't recommend throwing a 404. Use the canonical link tag in the instead if your worried about Google indexing both http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html.
But if you really want to I guess you could write a function that is called during the pre_controller hook http://codeigniter.com/user_guide/general/hooks.html that checks to see if the URL has an integer as the second segment then call the show_404() method. Perhaps a better solution when writing this function would be to redirect to the SEO friendly version.
Is there a way (via routing) in CodeIgniter to change:
example.com/category/4 to example.com/category/foo-bar
where Foo Bar is the name of category 4 in the database?
Yes.
Using CI 3,
http://www.codeigniter.com/user_guide/general/routing.html
Use Callbacks, PHP >= 5.3
$route['products/([a-zA-Z]+)/edit/(\d+)'] = function ($product_type, $id)
{
return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
You can route to call a function to extract the name of the category.
Hope I answered your question and can help more people to like codeigniter as I believe it's speedy and light.
Slugs usage is important to make web application more secure which i think is important.
A better recommendation will be to use route to give you a better solution.
$route['(:any)/method/(:num)'] = 'Class/method';
or
$route['(:any)/method/(:num)'] = 'Class/method/$1';
$route['(:any)/gallery/(:num)'] = 'Class/gallery/$1';
base_url()/business-services/gallery/6
base_url()/travel/gallery/12
how to modify routes in codeigniter
Have fun :)

Resources