Is there any possibility to reduce routes in laravel4 - laravel

I want to know is there any possibility to reduce routes for same controller in laravel4.
Here is my route:
Route::get('emp/add-employee/','EmpController#addEmployee');
Route::post('emp/add-employee/','EmpController#addEmployee');
Route::get('emp/edit-employee/{id}','EmpController#editEmployee');
Route::post('emp/edit-employee/{id}','EmpController#editEmployee');
Route::get('emp/view-employee/{id}','EmpController#viewEmployee');
is there any posibility to do reduce...?

Your route actions look like the ones you'd find in a RESTful Resource Controller. So you could use this:
Route::resource('emp', 'EmpController', array('only' => array('create', 'store', 'edit', 'update', 'show')));
This will of course require you to rename the controller methods accordingly and the route paths will be a little different, but you'd have a more compact route definition and consistent naming. Below are the routes that are generated by the Route::resource definition above.
+-----------------------------+---------------+-------------------------+
| GET emp/create | emp.create | EmpController#create |
| POST emp | emp.store | EmpController#store |
| GET emp/{id} | emp.show | EmpController#show |
| GET emp/{id}/edit | emp.edit | EmpController#edit |
| PUT emp/{id} | emp.update | EmpController#update |
+-----------------------------+---------------+-------------------------+
So you'd have to rename your controller method names like so:
GET : addEmployee() -> create() // shows the add form
POST: addEmployee() -> store() // processes the add form when submitted
GET : editEmployee() -> edit() // shows the edit form
POST: editEmployee() -> update() // processes the edit form when submitted
GET : viewEmployee() -> show()

You could use controller routes.
Route::controller('emp', 'EmpController');
Now you just have to rename the functions within your controller to also represent the method used like this:
public function getAddEmloyee()
public function postAddEmloyee()
public function getEditEmployee($id)
etc.
See also the Laravel docs for controllers

Yes, use Route::match(). This will allow you to specify GET and POST in a single route call, like so:
Route::match(['GET', 'POST'], 'emp/edit-employee/{id}','EmpController#editEmployee');
You can also use Route::all() which will match any type request, which includes GET and POST and also any other HTTP verbs that may be specified, if that's what you want.

Related

Not found error on get url with additive parameters

In laravel 6 I need to run control action with url like :
http://127.0.0.1/ads/showad?wid=207ce8c0-b153-11eb-b997-4d6d53720d0c&subid=
In routes/web.php I have:
Route::get('ads/showad', 'DisplayAdController#showad')->name('show_ad');
I got not found error
I tried to modify this url definition as :
Route::get('ads/showad?wid={wid}&subid={subid?}', 'DisplayAdController#showad')->name('show_ad');
and after clearing cache running command :
php artisan route:list
I see in output :
GET|HEAD | ads/showad | show_ad | App\Http\Controllers\DisplayAdController#showad
| GET|HEAD | ads/showad?wid={wid}&subid={subid?} | show_ad | App\Http\Controllers\DisplayAdController#showad |
Anyway I got Not Found error.
How have I to modify url defintion to run url, which must be run from external apps?
Thanks!
Your second route is unnecessary.
In your controller you can access to the query parameters like this:
public function showad(Request $request) {
$request->get('wid');
}
you can also use url parameters like this:
Route::get('ads/showad/{wid}/{subid}', 'DisplayAdController#showadById')->name('show_ad_by_id');
In this case you need to do this:
public function showadById($wid, $subid) {
}

Implicit Route Model Binding returns empty model in package

I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).
api.php:
Route::apiResources([
'trackers' => TrackerController::class,
'tracker/entry' => TrackerEntryController::class,
'tracker/types' => TrackerTypeController::class
]);
pertinent artisan route:list output:
| Method | URI | Middleware |
+-----------+-----------------------+------------+
| GET|HEAD | tracker/entry/{entry} | |
| GET|HEAD | tracker/types/{type} | |
| GET|HEAD | trackers/{tracker} | |
+-----------+-----------------------+------------+
Show method from TrackerTypeController:
use Oxthorn\Trackers\Models\TrackerType as Type;
public function show(Type $type)
{
return $type;
}
So, as far as I see, my code is using the correct naming scheme for implicit route binding.
If I change the controller show method to this:
public function show(Type $type, $id)
{
$type2 = Type::findOrFail($id);
return [
[get_class($type), $type->exists, $type],
[get_class($type2), $type2->exists],
];
}
I get this output:
[
[
"Oxthorn\\Trackers\\Models\\TrackerType",
false,
[]
],
[
"Oxthorn\\Trackers\\Models\\TrackerType",
true
]
]
This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.
You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:
Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');

redirect from one controller to another

My Codeigniter route.php is set to :
$route['default_controller'] = 'welcome/view';
I am trying to redirect to a test controller from the default controller's view method using redirect('test') but I am getting a Fatal error: Uncaught TypeError: Argument 1 passed to CI_Exceptions::show_exception() must be an instance of Exception, instance of Error given .
class Welcome extends CI_Controller {
public function view()
{
redirect('test');
}
}
-
class Test extends CI_Controller {
public function index()
{
echo 'hi';
}
}
I am expecting hi as the output as I am redirecting to the test controller . I am not sure what the error actually means . Could somebody please tell me what does the error mean and what is going wrong ?
For any of the functions in CodeIgniter's URL Helper (e.g. redirect(), base_url(), etc. ) to work the configuration item $config['base_url'] must be set.
The notes with this setting in /application/config/config.php says
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
Pay particular attention to the line
WARNING: You MUST set this value!
and don't forget the trailing slash.
Try this
redirect(base_url('Test/index'));
define route for test controller also in your config/routes.php file.
$route['test']='Test/index';
And redirect this url.
redirect(base_url('test'), 'refresh');

Controller actions appearing in "artisan route:list" as Closures

I'm at a loss. I'm working on upgrading an application to Laravel 5.4, but on the way (5.2.45) I've got something weird happening with several routes. I had 3 Route::controller() calls in routes.php, and I've converted all of them to explicit routes since ::controller() has been dropped. Now the new routes from one of the ::controller() calls are working fine, the routes from the other two are not, reporting action not found.
Looking at examples, here's one that is working:
\Route::get( 'account/edit', array( 'as'=>'account.edit', 'uses'=>'AccountController#getEdit' ) );
and here is one that is not:
\Route::get( 'recent/clear', array( 'as'=>'recent.clear', 'use'=>'RecentController#getClear' ) );
For completeness sake, here are the applicable Controller sections
class AccountController extends Controller {
public function getEdit()
{
$roles = Role::getSelectList();
$districts = District::getSelectList();
return \View::make('accountedit', array( 'editUser' => \Auth::user(), 'roles'=>$roles, 'districts'=>$districts ));
}
}
class RecentController extends Controller {
public function getClear()
{
\Session::forget( 'recent' );
return \Redirect::to('/main')->with( array( 'alerts'=>array( ErrorHelper::alert('success','Success','The Recent list has been cleared.') ) ) );
}
}
The most direct sign something weird is going on is when I run artisan route:list. Here are the two applicable lines from the results:
| Method | URI | Name | Action |
+-----------+---------------+---------------------------------+-------------------------------------------------+
| GET|HEAD | account/edit | account.edit | App\Http\Controllers\AccountController#getEdit |
| GET|HEAD | recent/clear | recent.clear | Closure |
I have no idea why recent.clear is showing up as a Closure. Any thoughts as to what's wrong? FWIW, I have identical namespacing on both controllers, which are both in the Http\Controllers directory.
Oh heck, uses vs. use. In the words of Grover, "I am so embarrassed..."

Codeigniter slug, url

I have 4 controllers: Catalog, Contact, About, Main.
On each controller, I want to set the individual URL from the database.
The structure of the database page:
url_title|id_page|name_page|page_section|
about-us | 5 | About | about |
how can this be implemented, taking into account that the function will be also indicate the URL?
the site url can be different then the controller name and optional method name.
in application/config/routes.php
$route['about-us'] = 'about';
so then website.com/about-us will 'route' to the about controller index method
if you want to specify a method in the about controller such as show()
$route['about-us'] = 'about/show';

Resources