I am working on an existing laravel project. Problem is whenever I create a new route the new route does not function. Also I am not receiving this error
GET http://localhost/khawaja%20sons/khawjapos.unialsolutions.com/public/products/warehouse 404 (Not Found).
Routes
ProductController
link from where I am using the route
Output
You have a two option how to use routes after you are set them:
first one :
Link
You have tried this method but, you need to remove asset function, because asset function return path to public folder
The second way to use route and the way I recommend is to use route names
Link
I recommend this way, because if you change the path in future, your change will be applied to everywhere you use route name, you don't need to be worry is route will work
I have tried both options before. Both are giving the same error
Related
I am developing a Web Api using Laravel. In the routes/api.php file, I am trying to retrieve the header value. But it is throwing the error.
First I retrieve like this.
use Illuminate\Http\Request;
Request::header('api-version')
But, the error is saying that the method should not be called statically.
Then I used this one.
use Request;
Request::header('api-version')
When I tried the second one, it gave me this error.
The use statement with non-compound name 'Request' has no effect
So, how can I retrieve header value inside api route file in Laravel?
I am trying to get api versioning in place for an API I am working on, I found this post that explained how to do it using middleware and replacing a string in the route itself. Basically specifying routes like this.
Route::group(['middleware' => ['api-version']], function() {
Route::get('/endoint', ['uses' => '{api-namespace}\EndpointController#endpoint']);
});
However, when I attempt this I get the following error
Class App\Http\Controllers\{api-namespace}\EndpointController does not exist
It would appear that the container is verifying the existence of route controller files prior to running the middleware which does the replacing. I have added the middleware to the $routeMiddleware in the Http Kernel file.
How can I accomplish this before it checks for the existence of the file?
I thought about adding this to the applications global middleware but I do not want this to run on web only on api calls
Create a different file for next version of API has some downside.
You have to create all the routes from version 1
and in my case version 2 was just some changes to 3 requests. that was the time I felt we need a fallback for this kind of operation.
then I created a simple Laravel package to support Laravel API versioning it adds fallback functionality to routes. I personally needed this long ago but didn't realize it will be achieved with such a tiny package.
https://github.com/mbpcoder/laravel-api-versioning
The problem is that uses actually tries to retrieve a class and then call the method inside, you shouldn't be encouraged to put any parameters there so restrain from doing so, instead try grouping your api routes under certain prefix and middleware like so:
Route::prefix('XXXXXXX')->group(['middleware' => ['api-version']], function() {
Route::get('/endoint', 'EndpointController#endpoint');
});
Note: My above assumption is made out of that you didn't handle changing {api-namespace} inside of your middleware class properly.
Stepping through the code allowed me to see that this is already handled by Laravel and all i needed to do was create a routes/api/v2.php file with the routes for version 2. The only problem I see is having to duplicate all routes which did not change from version 1 to version 2. I may look into modifying my RouteServiceProvider to actually inherit previous versions if they are not overridden in the requested api version rather than duplicating the routes code for every api version.
I have two separate Laravel instances/sites running on a server and want to be able to generate a url to a named route on one from code within the other.
For example the following named route exists in the first instance:
Route::get('users/my_account', array('uses' => 'UsersController#myAccount', 'as' => 'my_account'))
In the second instance I want to generate a url to the route above. Can anyone think of a clean way to do this, without explicitly knowing the url (i.e. only knowing the name of the route 'my_account')?
Basically I want to expose the RouteCollection of one site to the other...
That's a pretty interesting question. There's no natively supported way of doing that and, from what I know, it won't ever.
You could try loading the routes file of the first application, parsing it's configurations (you will need that for reverse routing), construct a Router instance and use it, but I'm sure it won't be simple at all.
If you have a really, I mean really, good reason to use reverse routes, you can try building a small API on the first application. That API should receive the parameters necessary, those used in url($params), and return the full url (with domain and everything). Although, this will introduce some serious performance issues.
IMHO, stick to hard coded my_account, leave a comment on the first application route and/or controller explaining that it's used on another project and move on :)
I may call this a dirty trick. Supposing you have the following structure in your file system, where both paths are Laravel apps:
/path/to/apps/app1
/path/to/apps/app2
And you want to load the routes file from app1 into app2. You can do it as follow:
include "../app1/apps/routes.php";
$url = URL::route('register');
VoilĂ ! But, although it worked for me there are some points to consider.
Include that file, will surely overwrites your current route collection for that process.
If that last is true, then you can have problem generating other URLs, maybe in your views.
The domain name will be of the current Laravel instance. It means that your URLs generated into app2 will hold the domain name of app1. I believe this is not what you want. But you can always generate non-absolute URLs with URL::route('register', null, false).
I am using HMVC and created a module called user. Inside modules/user/config directory, I have routes.php, using the same format as application/config/routes.php.
In application/config/routes.php I have the following route:
$route['login'] = 'user/login';
This works great, buit when I move it to application/modules/user/config/routes.php, it does not work. I get a 404 error.
According to HMVC docs (https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc), you have to create routes as follows:
$route['module_name'] = 'controller_name';
This means I will have to do it like this:
$route['user'] = 'user';
This causes the 404, however, even if I did not get the 404, this is not quite what I have in mind. I still want to keep my routing to work as /login goes to user/login.
Any ideas would be greatly appreciated!
Thanks!
I had the same exact problem as you, unfortunately the way Wiredesignz created the extension it requires that the path start with the module name itself if you put the routes file inside the module itself. That is the only way it will look at the routes file if it is placed inside a module. With that said it already knows the module name at that point, so you need to simply specify the controller and method that you want it to route to. So in your routes.php file inside of your module config directory, if you put this:
$route['yourmodule/some-route'] = "yourcontroller/yourmethod";
or in other words:
$route['user'] = 'user/login';
That would work I believe. However I still wanted more than this. I wanted to be able to use routes that may or may not have the module name. Due to this I had to extend the module to make this happen, and you can find the work I did here if this helps:
https://github.com/brianwozeniak/codeigniter-modular-extensions-hmvc
This would then allow you to use the route you wanted such as:
$route['login'] = 'user/login';
Even with that routes.php placed inside the module's config directory.
I'm working on a website that have virtual hosts, like xyz.com, asd.com and so on.
I use Hostname route like ":controller.com", so it routes me to the specific controller.
I created a defaultController to a default behavior and I extended xyzController and asdController from that, so I only modify that I really want.
The problem is that, how can I create my DefaultController to be really default :) I mean, if I haven't created a controller for the specific route (forexample: xyzController for xyz.com), then I don't want to get a Fatal Error like
"Fatal error: Class 'aaaa\Controller\asdController' not found in /var/www/samba/aaaa/www/src/site/vendor/ZF2/library/Zend/ServiceManager/AbstractPluginManager.php on line 177"
Would be better if it route me to defaultController immediately.
I was thinking about the view_manager's templates like 'not_found_template'. That would be great if not_found_controller would exist :)
Is it possible to do somehow?